Passing a ParamArray to an ActiveX Object Method 
Author Message
 Passing a ParamArray to an ActiveX Object Method

Does anyone know what OLE type VB passes for a ParamArray?  (Which I believe
is really just a variant array.)

More specifically, what is the IDL incantation required to make VB pass a
variable argument list to an ActiveX method correctly?

I can successfully pass a variant array as a simple variant.  The trick
seems to be convincing VB to allow me to use the variable argument syntax to
call the method.

I have tried:

  [vararg, id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
VARIANTARG VarArg);

VB Likes this okay, but when I get into the method on the other side, the
data does not look like a variant at all.

...and I have tried...

 [vararg, id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
SAFEARRAY(VARIANT) pVarArg);

VB hates this.  He says that the method is restricted or the method is using
an automation type not supported by VB.

...I have also tried...

  [id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
VARIANTARG VarArg);

But then VB does not know that the argument list is variable in this case.

Any ideas?

--
Erick Kobres



Sun, 15 Oct 2000 03:00:00 GMT  
 Passing a ParamArray to an ActiveX Object Method

Erick,

Try defining the routine in IDL something like this:

HRESULT TestFunc([in, out] SAFEARRAY(VARIANT)* Params);

--
Ben Baird, MVP
Microsoft SiteBuilder Network Level 2
Visual Basic Thunder, v. 2.0
http://www.{*filter*}highway.com/~psy/
Common Controls Replacement Project, Official Member
http://www.*-*-*.com/
Please direct your programming questions to the newsgroups.

Quote:

>Does anyone know what OLE type VB passes for a ParamArray?  (Which I
believe
>is really just a variant array.)

>More specifically, what is the IDL incantation required to make VB pass a
>variable argument list to an ActiveX method correctly?

>I can successfully pass a variant array as a simple variant.  The trick
>seems to be convincing VB to allow me to use the variable argument syntax
to
>call the method.

>I have tried:

>  [vararg, id(8), helpstring("method TestVariant")] HRESULT
TestVariant([in]
>VARIANTARG VarArg);

>VB Likes this okay, but when I get into the method on the other side, the
>data does not look like a variant at all.

>...and I have tried...

> [vararg, id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>SAFEARRAY(VARIANT) pVarArg);

>VB hates this.  He says that the method is restricted or the method is
using
>an automation type not supported by VB.

>...I have also tried...

>  [id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>VARIANTARG VarArg);

>But then VB does not know that the argument list is variable in this case.

>Any ideas?



Sun, 15 Oct 2000 03:00:00 GMT  
 Passing a ParamArray to an ActiveX Object Method

Hi

I ran into this problem myself.

First you have to create a safe array of the type you want to pass. Then
assign the values from your array to the elements of the safe array. Create
a variant variable and set its type to array and the type for your array
element and pass this variant variable to the ActiveX function. I have no
idea about VB but in VC the code looks like this

 SAFEARRAY FAR *pSafeArray;
 SAFEARRAYBOUND rgsabound[1];
 VARIANT VarArr;
 double *pdArr;

 VariantInit(&VarArr);

 // create the safe bound array
 rgsabound[0].lLbound = 0;
 rgsabound[0].cElements = nCount;
 pSafeArray = SafeArrayCreate(VT_R8, 1, rgsabound);
 if(pSafeArray)
 {
     pdArr = (double *)pSafeArray->pvData;
     VarArr.parray = pSafeArray;
     VarArr.vt = VT_ARRAY|VT_R8;

    /*....
    ... set values to elements of safe array
    ....*/

     ActiveXObject.SendArray(VarArr);

Quote:
}

I hope this helps

Prady

Quote:

>Does anyone know what OLE type VB passes for a ParamArray?  (Which I
believe
>is really just a variant array.)

>More specifically, what is the IDL incantation required to make VB pass a
>variable argument list to an ActiveX method correctly?

>I can successfully pass a variant array as a simple variant.  The trick
>seems to be convincing VB to allow me to use the variable argument syntax
to
>call the method.

>I have tried:

>  [vararg, id(8), helpstring("method TestVariant")] HRESULT
TestVariant([in]
>VARIANTARG VarArg);

>VB Likes this okay, but when I get into the method on the other side, the
>data does not look like a variant at all.

>...and I have tried...

> [vararg, id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>SAFEARRAY(VARIANT) pVarArg);

>VB hates this.  He says that the method is restricted or the method is
using
>an automation type not supported by VB.

>...I have also tried...

>  [id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>VARIANTARG VarArg);

>But then VB does not know that the argument list is variable in this case.

>Any ideas?

>--
>Erick Kobres



Mon, 16 Oct 2000 03:00:00 GMT  
 Passing a ParamArray to an ActiveX Object Method

Thanks for this info.

I was actually looking for the IDL required to build the typelib such I
would be able to have VB do the following, for example:

Dim X as new XTest

X.TestParamArray "Here is a string"
X.TestParamArray "This", "Is", "a", "test", 1, 2, 3, 3.141, False

Obviously VB will want to send something like this as a SAFEARRAY of
VARIANTS.

I did, through experimentation, finally find something that seems to work.
I have to say I'm not toally happy with it as it involves passing a pointer
to a pointer though OLE, which seems unnecessary.  Apparently though, this
is what VB wants to see.

The line of IDL I ended up with was this:

[vararg, id(8), helpstring("method TestParamArray")] HRESULT
TestParamArray([in, out] SAFEARRAY(VARIANT) *ppsaParams);

The corresponding ATL method looks like this:

STDMETHODIMP CXTest::TestParamArray(struct tagSAFEARRAY **ppsaParams)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState())

   VARIANT * pVariants;
   HRESULT hr;

   hr = SafeArrayAccessData(*ppsaParams, (void**) &pVariants);
   if (SUCCEEDED(hr))
      {
      // ... process variants here....
      SafeArrayUnaccessData(*ppsaParams);
      }

   return S_OK;

Quote:
}

Too many stars for my liking, but it seems to be what VB requires.

Quote:

>Hi

>I ran into this problem myself.

>First you have to create a safe array of the type you want to pass. Then
>assign the values from your array to the elements of the safe array. Create
>a variant variable and set its type to array and the type for your array
>element and pass this variant variable to the ActiveX function. I have no
>idea about VB but in VC the code looks like this

> SAFEARRAY FAR *pSafeArray;
> SAFEARRAYBOUND rgsabound[1];
> VARIANT VarArr;
> double *pdArr;

> VariantInit(&VarArr);

> // create the safe bound array
> rgsabound[0].lLbound = 0;
> rgsabound[0].cElements = nCount;
> pSafeArray = SafeArrayCreate(VT_R8, 1, rgsabound);
> if(pSafeArray)
> {
>     pdArr = (double *)pSafeArray->pvData;
>     VarArr.parray = pSafeArray;
>     VarArr.vt = VT_ARRAY|VT_R8;

>    /*....
>    ... set values to elements of safe array
>    ....*/

>     ActiveXObject.SendArray(VarArr);
>}

>I hope this helps

>Prady


>>Does anyone know what OLE type VB passes for a ParamArray?  (Which I
>believe
>>is really just a variant array.)

>>More specifically, what is the IDL incantation required to make VB pass a
>>variable argument list to an ActiveX method correctly?

>>I can successfully pass a variant array as a simple variant.  The trick
>>seems to be convincing VB to allow me to use the variable argument syntax
>to
>>call the method.

>>I have tried:

>>  [vararg, id(8), helpstring("method TestVariant")] HRESULT
>TestVariant([in]
>>VARIANTARG VarArg);

>>VB Likes this okay, but when I get into the method on the other side, the
>>data does not look like a variant at all.

>>...and I have tried...

>> [vararg, id(8), helpstring("method TestVariant")] HRESULT
TestVariant([in]
>>SAFEARRAY(VARIANT) pVarArg);

>>VB hates this.  He says that the method is restricted or the method is
>using
>>an automation type not supported by VB.

>>...I have also tried...

>>  [id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>>VARIANTARG VarArg);

>>But then VB does not know that the argument list is variable in this case.

>>Any ideas?

>>--
>>Erick Kobres



Mon, 16 Oct 2000 03:00:00 GMT  
 Passing a ParamArray to an ActiveX Object Method

Well the idl entrly for the example I had sent earlier would be

[id(1), helpstring("Pass an array to this function")] HRESULT SendArray([in]
VARIANT);

Prady

Quote:

>Thanks for this info.

>I was actually looking for the IDL required to build the typelib such I
>would be able to have VB do the following, for example:

>Dim X as new XTest

>X.TestParamArray "Here is a string"
>X.TestParamArray "This", "Is", "a", "test", 1, 2, 3, 3.141, False

>Obviously VB will want to send something like this as a SAFEARRAY of
>VARIANTS.

>I did, through experimentation, finally find something that seems to work.
>I have to say I'm not toally happy with it as it involves passing a pointer
>to a pointer though OLE, which seems unnecessary.  Apparently though, this
>is what VB wants to see.

>The line of IDL I ended up with was this:

>[vararg, id(8), helpstring("method TestParamArray")] HRESULT
>TestParamArray([in, out] SAFEARRAY(VARIANT) *ppsaParams);

>The corresponding ATL method looks like this:

>STDMETHODIMP CXTest::TestParamArray(struct tagSAFEARRAY **ppsaParams)
>{
>   AFX_MANAGE_STATE(AfxGetStaticModuleState())

>   VARIANT * pVariants;
>   HRESULT hr;

>   hr = SafeArrayAccessData(*ppsaParams, (void**) &pVariants);
>   if (SUCCEEDED(hr))
>      {
>      // ... process variants here....
>      SafeArrayUnaccessData(*ppsaParams);
>      }

>   return S_OK;
>}

>Too many stars for my liking, but it seems to be what VB requires.


>>Hi

>>I ran into this problem myself.

>>First you have to create a safe array of the type you want to pass. Then
>>assign the values from your array to the elements of the safe array.
Create
>>a variant variable and set its type to array and the type for your array
>>element and pass this variant variable to the ActiveX function. I have no
>>idea about VB but in VC the code looks like this

>> SAFEARRAY FAR *pSafeArray;
>> SAFEARRAYBOUND rgsabound[1];
>> VARIANT VarArr;
>> double *pdArr;

>> VariantInit(&VarArr);

>> // create the safe bound array
>> rgsabound[0].lLbound = 0;
>> rgsabound[0].cElements = nCount;
>> pSafeArray = SafeArrayCreate(VT_R8, 1, rgsabound);
>> if(pSafeArray)
>> {
>>     pdArr = (double *)pSafeArray->pvData;
>>     VarArr.parray = pSafeArray;
>>     VarArr.vt = VT_ARRAY|VT_R8;

>>    /*....
>>    ... set values to elements of safe array
>>    ....*/

>>     ActiveXObject.SendArray(VarArr);
>>}

>>I hope this helps

>>Prady




- Show quoted text -

Quote:
>>>Does anyone know what OLE type VB passes for a ParamArray?  (Which I
>>believe
>>>is really just a variant array.)

>>>More specifically, what is the IDL incantation required to make VB pass a
>>>variable argument list to an ActiveX method correctly?

>>>I can successfully pass a variant array as a simple variant.  The trick
>>>seems to be convincing VB to allow me to use the variable argument syntax
>>to
>>>call the method.

>>>I have tried:

>>>  [vararg, id(8), helpstring("method TestVariant")] HRESULT
>>TestVariant([in]
>>>VARIANTARG VarArg);

>>>VB Likes this okay, but when I get into the method on the other side, the
>>>data does not look like a variant at all.

>>>...and I have tried...

>>> [vararg, id(8), helpstring("method TestVariant")] HRESULT
>TestVariant([in]
>>>SAFEARRAY(VARIANT) pVarArg);

>>>VB hates this.  He says that the method is restricted or the method is
>>using
>>>an automation type not supported by VB.

>>>...I have also tried...

>>>  [id(8), helpstring("method TestVariant")] HRESULT TestVariant([in]
>>>VARIANTARG VarArg);

>>>But then VB does not know that the argument list is variable in this
case.

>>>Any ideas?

>>>--
>>>Erick Kobres



Fri, 20 Oct 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Overloaded Method (ParamArray issue)

2. How do I pass recordset to ParamArray?

3. Pass ParamArray to User Defined Function

4. HowTo?: Pass an Array into a ParamArray parameter

5. Passing an Array to a ParamArray

6. Passing an array as a ParamArray arguement

7. passing array to ParamArray arg?

8. Passing ParamArrays between routines

9. Passing ParamArray to subfunction

10. Passing around ParamArray

11. How do I pass an array to ActiveX control method

12. Passing interfaces to ActiveX methods

 

 
Powered by phpBB® Forum Software