Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variant of type VT_INT as a function argument

Status
Not open for further replies.

onlyshiza

Programmer
May 14, 2003
18
US
Hi everyone,

I am calling a method in a COM dll that as the first parameter takes a VARIANT of type VT_INT (actually it has to be like a VARIANT array of (1,0,0), see the following code for more clarification). I am trying to make it work in C++. I have the equivalent working VB code which is like:(in the following code pM is the pointer to an interface)

dim XVect
XVect = pM.RotateVector(Array(1, 0, 0))

and it works perfect, i.e the Array function in VB does the trick.

Now in C++, I am trying this:

_variant_t var;
VariantInit(&var);
var.vt = VT_ARRAY | VT_INT;
var.parray= (1,0,0);
//pM is a smart pointer to an interface
pM->RotateVector(&var);

But I am getting an Unknown error on the last line (I have tried it with VARIANT instead of _variant_t as well). Any ideas of what I am doing wrong here? I thought maybe I have to first put my array of (1,0,0) in a safeArray and then assign the safearray to var.parray before passing var to the function. Am I way off?
Appreciate any comments.

onlyshiza

 
I'm not sure how to use the variant_t, because I always use plain VARIANTS. In the VARIANT way of programming you need to create a SAFEARRAY and set the parray value of the VARIANT to the pointer returned by SafeArrayCreate().

So my guess is that you're not that far off....

Greetings,
Rick
 
thanks for your reply Rick,

Now I am using the VARIANT and setting the value of it from a SAFEARRAY like this:

Code:
int tempArray[3]={1,0,0};	
VARIANT var;
VariantInit(&var);
var.vt = VT_ARRAY | VT_INT;
SAFEARRAY* pSafArr;
SAFEARRAYBOUND safArrBnds = {3, 0};
pSafArr = SafeArrayCreate(VT_INT, 1, &safArrBnds);   
int* inputArray= new int[3];		
SafeArrayAccessData(pSafArr, reinterpret_cast<void**>(&inputArray));
for(int i = 0; i < 3; i++)
  inputArray[i] = tempArray[i];
SafeArrayUnaccessData(pSafArr);
var.parray = pSafArr;
//The following line throws an unknown error-
//pM is a smart pointer to an interface
pM->RotateVector(&var);

The interesting thing is if I extract the values stored in var before calling the RotateVector method, the values comeup correctly (1,0,0), so the SAFEARRAY and var are doing their job. I am sure the smart pointer to the interface (pM) is valid as I am calling other methods from it.

this is the prototype of the RotateVector in VB (which as I mentioned before is working perfectly in VB)
Code:
Public Function RotateVector(ByVal InVector As Variant) As Variant

Any ideas what may be wrong?

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top