Passing an array of Strings to VB via ActiveX 
Author Message
 Passing an array of Strings to VB via ActiveX

Hi folks,

I'm trying to pass a string of arrays to a VB ActiveX control. The VB
method takes a Variant as
an input parameter, checks with IsArray for the correct content and then
accesses individual array
elements. Using the code snippet below, it does well recognize the
number of elements in the
array, but not their content -- it displays empty strings.

What's wrong with it? Is there a better way to do these things using MFC
wrapper classes?
Or even ATL?

Please respond also by email.

Regards,
Lars

void CCheopsDlg::OnRefreshPalette() // responds to an event from the AX
control
{
 VARIANT varNames;
 varUnitNames.vt = VT_BSTR | VT_ARRAY;
 SAFEARRAYBOUND sfaBound;
 long FAR *index = new long[1];

 long lNumNames = 2;
 _bstr_t FAR *n1   = new _bstr_t("Name1");
 _bstr_t FAR *n2 = new _bstr_t("Name2");

 sfaBound.cElements = lNumNames;
 sfaBound.lLbound = 0;

 varNames.parray = SafeArrayCreate (VT_BSTR, 1, &sfaBound);

 index[0] = 0;
 SafeArrayPutElement (varNames.parray, index, n1);

 index[0] = 1;
 SafeArrayPutElement (varNames.parray, index, n2);

 // call the Visual Basic function
 m_control.InitPalette (&varNames);

 SafeArrayDestroy (varNames.parray);

Quote:
}



Sat, 26 May 2001 03:00:00 GMT  
 Passing an array of Strings to VB via ActiveX
Hi Lars,

I fail to see a problem with your code on the C++ side. Verify that the
ActiveX control is looking at the array as an array of pointers (BSTR).

Good luck!

--
Carlos Portela, Simple Software Solutions, Inc.
e_Db 3.1 - Portable, fast, reliable database engine
e_Fs 2.0 - Portable structured storage
e_Comm 2.0 - Multi-platform, client-server communications
http://www.simple-sw.com



Sat, 26 May 2001 03:00:00 GMT  
 Passing an array of Strings to VB via ActiveX
Refer to www.codeguru.com/atl there is an example of passing strings from
server to client (rather there are two examples), They  work.
Quote:

>Hi folks,

>I'm trying to pass a string of arrays to a VB ActiveX control. The VB
>method takes a Variant as
>an input parameter, checks with IsArray for the correct content and then
>accesses individual array
>elements. Using the code snippet below, it does well recognize the
>number of elements in the
>array, but not their content -- it displays empty strings.

>What's wrong with it? Is there a better way to do these things using MFC
>wrapper classes?
>Or even ATL?

>Please respond also by email.

>Regards,
>Lars

>void CCheopsDlg::OnRefreshPalette() // responds to an event from the AX
>control
>{
> VARIANT varNames;
> varUnitNames.vt = VT_BSTR | VT_ARRAY;
> SAFEARRAYBOUND sfaBound;
> long FAR *index = new long[1];

> long lNumNames = 2;
> _bstr_t FAR *n1   = new _bstr_t("Name1");
> _bstr_t FAR *n2 = new _bstr_t("Name2");

> sfaBound.cElements = lNumNames;
> sfaBound.lLbound = 0;

> varNames.parray = SafeArrayCreate (VT_BSTR, 1, &sfaBound);

> index[0] = 0;
> SafeArrayPutElement (varNames.parray, index, n1);

> index[0] = 1;
> SafeArrayPutElement (varNames.parray, index, n2);

> // call the visual basic function
> m_control.InitPalette (&varNames);

> SafeArrayDestroy (varNames.parray);
>}



Sat, 26 May 2001 03:00:00 GMT  
 Passing an array of Strings to VB via ActiveX
Hi, your VB method should be declared as

Sub InitPallette(ParamArray arrayItems() As String)

 ...guts...

End Sub

Quote:

>Hi folks,

>I'm trying to pass a string of arrays to a VB ActiveX control. The VB
>method takes a Variant as
>an input parameter, checks with IsArray for the correct content and then
>accesses individual array
>elements. Using the code snippet below, it does well recognize the
>number of elements in the
>array, but not their content -- it displays empty strings.

>What's wrong with it? Is there a better way to do these things using MFC
>wrapper classes?
>Or even ATL?

>Please respond also by email.

>Regards,
>Lars

>void CCheopsDlg::OnRefreshPalette() // responds to an event from the AX
>control
>{
> VARIANT varNames;
> varUnitNames.vt = VT_BSTR | VT_ARRAY;
> SAFEARRAYBOUND sfaBound;
> long FAR *index = new long[1];

> long lNumNames = 2;
> _bstr_t FAR *n1   = new _bstr_t("Name1");
> _bstr_t FAR *n2 = new _bstr_t("Name2");

> sfaBound.cElements = lNumNames;
> sfaBound.lLbound = 0;

> varNames.parray = SafeArrayCreate (VT_BSTR, 1, &sfaBound);

> index[0] = 0;
> SafeArrayPutElement (varNames.parray, index, n1);

> index[0] = 1;
> SafeArrayPutElement (varNames.parray, index, n2);

> // call the visual basic function
> m_control.InitPalette (&varNames);

> SafeArrayDestroy (varNames.parray);
>}



Sat, 26 May 2001 03:00:00 GMT  
 Passing an array of Strings to VB via ActiveX
Hi Lars

Are you not passing _bstr_t pointers instead of BSTR pointers ? Maybe you
should use the _bstr_t.copy() member ?
You fail to set the varNames.parray member to your new array.

How about:

VARIANT varNames;
VariantInit(&varNames);
varNames.vt = VT_BSTR | VT_ARRAY;
SAFEARRAYBOUND sfaBound = {2, 0};
SAFEARRAY* parr = SafeArrayCreate(VT_BSTR, 1, &sfaBound);
BSTR* bstrArray;
SafeArrayAccessData(parr, (void**) &bstrArray);
_bstr_t n1("Name1");
_bstr_t n2("Name2");
bstrArray[0] = n1.copy();
bstrArray[1] = n2.copy();
SafeArrayUnaccessData(parr);
varNames.parray = parr;

Quote:
> m_control.InitPalette (&varNames); <

regards,
Niels Hede Pedersen

Quote:

>Hi folks,

>I'm trying to pass a string of arrays to a VB ActiveX control. The VB
>method takes a Variant as
>an input parameter, checks with IsArray for the correct content and then
>accesses individual array
>elements. Using the code snippet below, it does well recognize the
>number of elements in the
>array, but not their content -- it displays empty strings.

>What's wrong with it? Is there a better way to do these things using MFC
>wrapper classes?
>Or even ATL?

>Please respond also by email.

>Regards,
>Lars

>void CCheopsDlg::OnRefreshPalette() // responds to an event from the AX
>control
>{
> VARIANT varNames;
> varUnitNames.vt = VT_BSTR | VT_ARRAY;
> SAFEARRAYBOUND sfaBound;
> long FAR *index = new long[1];

> long lNumNames = 2;
> _bstr_t FAR *n1   = new _bstr_t("Name1");
> _bstr_t FAR *n2 = new _bstr_t("Name2");

> sfaBound.cElements = lNumNames;
> sfaBound.lLbound = 0;

> varNames.parray = SafeArrayCreate (VT_BSTR, 1, &sfaBound);

> index[0] = 0;
> SafeArrayPutElement (varNames.parray, index, n1);

> index[0] = 1;
> SafeArrayPutElement (varNames.parray, index, n2);

> // call the visual basic function
> m_control.InitPalette (&varNames);

> SafeArrayDestroy (varNames.parray);
>}



Sun, 27 May 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing VB pointer to C Short Array (via void *)

2. Passing String Arrays between VB and VC

3. passing (string) arrays from VB to C-DLL and vice versa

4. Passing an array of strings from VB to Windows API

5. Passing array of strings to a dll in VB

6. Passing VB String array to DLL???

7. Passing String Arrays between VB and VC

8. passing (string) arrays from VB to C-DLL and vice versa

9. passing (string) arrays from VB to C-DLL and vice versa

10. Passing an array of strings from VB to C

11. How to pass a string Array from VB to a C++ DLL

12. Pass array from ASP to VB ActiveX DLL

 

 
Powered by phpBB® Forum Software