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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays from a c++ client to a VB activex server

Status
Not open for further replies.

Mukiwa

Programmer
Feb 13, 2001
3
IL
Hi,

I am having trouble writing a c++ client, that is meant to send a variant object containing an array of strings to a VB activex.exe.
I created a VARIANT object pointing to a safearray of BSTR's.
On the VB side, the upper and lower bounds of the array are read correctly, but the data is empty.
When using a VB client I had no problems whatsoever.
What could I be doing wrong?

Thanks
David
 
You can receive, manipulate, and return Microsoft Visual Basic arrays in Microsoft Visual C++ with an understanding of safe arrays. A safe array is an array that contains information about the number of dimensions and the bounds of its dimensions. Because Microsoft Visual Basic natively uses these types of arrays, you have to construct safe arrays of the data you want to pass.
Normally, this is a difficult task, and hard to manage from Microsoft Visual C++. However, this task is easier if you use the ColeSafeArray class included with the Microsoft Foundation Classes. The following DLL routine demonstrates how to use this class to return a 10x10 two-dimensional array of doubles from a DLL.

NOTE: The same code and principles in this sample DLL also apply to an OLE server.

VARIANT _stdcall retVariantArray(void) {
COleSafeArray saRet;
DWORD numElements[] = {10, 10}; // 10x10

// Create the safe-array...
saRet.Create(VT_R8, 2, numElements);

// Initialize it with values...
long index[2];
for(index[0]=0; index[0]<10; index[0]++) {
for(index[1]=0; index[1]<10; index[1]++) {
double val = index[0] + index[1]*10;
saRet.PutElement(index, &amp;val);
}
}

// Return the safe-array encapsulated in a VARIANT
return saRet.Detach();
}

Regards,
Anbu.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top