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 an array to a COM dll function

Status
Not open for further replies.

whodaman

Programmer
May 23, 2001
60
CA
Hi all,

I tried creating a class that has a function that accepts an array. I am not sure what i'm doing wrong. I'm using VC++ .Net and trying to bring the COM into VB .Net. I see all my other functions except setArr (the one that passes the array).

Code:
// My class
	public __gc class Boxes
	{
	private:
		int maxPos;
		int *myArr;

	public:
		Boxes(int, int, int);
		~Boxes();
		void setArr(int*, int);
		int getArray(int);
	};

// Function that accpets Array and cannot be seen in VB.net
	void SimMap::Boxes::setArr(int *thisArray, int count)
	{
		maxPos = count;
		for (int i = 0; i<maxPos; i++)
		{
			//myArr[i] = thisArray[i];
			// Do something with thisArray
		} 
	}
// Regular function
	int SimMap::Boxes::getArray(int index)
	{
		return myArr[index];
	}

Thanks in advance
 
use SAFEARRAY

Ion Filipski
1c.bmp
 
I have tried doing this:

Code:
void SimMap::Boxes::setArr(int [b]__gc[/b] *thisArray, int count)

What's the difference between that and using SAFEARRAY?
 
As far as you want to pass it to a COM method or interface you should use SAFEARRAY. The __gc is only for .NET framework(ie managed C++, C#, VB .NET).

Ion Filipski
1c.bmp
 
The SAFEARRAY structure is the only safe way to pass array into COM environments.

You also have a bunch of API function to use with this array

like SafeArrayCreate, SafeArrayGetElement etc.

I suggest you find some sample with SAFEARRAYS(under .net if you can) first in order not to waste time because you are not so familiar with it.

oh, one more thing, the classical VB was UNABLE to pass or read arrays from COM objects without the help of SAFEARRAY and Variants. I have tried this and I got some sort of strange error at the time

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Hi Guys, thanks for your responses. I have been looking at safearrays and there is a good walkthrough that at under sample 1.

I managed to pass an array with pointers and safearrays in that tutorial. The problem I have now is passing 2 or more dimentional arrays. I see safearrays can do that but the problem with that is that everytime i want to access an element, I have to call the function SafeArrayGetElement. Using safearray in general would drasticly slow my program down which in turn defeats the purpose of me using C++.

Is there a way I can access a 2 or more dimension array via the pointer? For example:

Code:
long __declspec (dllexport) __stdcall AddLongs_Pointer(
long *plArrayOfLongs, // the array's pointer
long lElements) // number of elements to add
{
...
...
[b]plArrayOfLongs[i][j][/b] = someValue; 
// error C2109: subscript requires array or pointer type
}
 
Hi all,

I am trying to pass a 2d array though a DLL with the help of SafeArray. As mentioned above, using pointers is a dead end. So here is my code that tries to returs the value of ArrayOfLongs(x, y) into lSum.

In VB, I call the dll by:
' Function Declaration
Code:
Private Declare Function return_SafeArray Lib "C:safeArrays.dll" (ByRef ArrayOfLongs(,) As Integer, ByRef x As Integer, ByRef y As Integer, ByRef lSum As Integer) As Integer

Dim ArrayOfLongs(10, 10) As Integer	' 2D array
k = return_SafeArray(ArrayOfLongs, x, y, lSum)	' Function call!!!
This is the function in C++:
Code:
long __declspec (dllexport) __stdcall return_SafeArray(
    SAFEARRAY **psaArray, // the array to use
    long x, // value of the column
	long y,	// value of the row
	VARIANT FAR *plSum) // returns the value of the array
{
	long lDimensions[2]; // index of dimensions
	HRESULT lResult; // return code for OLE functions

	lDimensions[0] = x;
	lDimensions[1] = y;
	lResult = SafeArrayGetElement(*psaArray, lDimensions, plSum);
	if (lResult == E_INVALIDARG) return 10;
	if (lResult == E_OUTOFMEMORY) return 11;
	if (lResult == DISP_E_BADINDEX) return 12;
	if (lResult != S_OK) return (lResult);	// Return error msg
				
	return(0);		// Returning No Error
}

After i call the function, what is returned is 12 which is: DISP_E_BADINDEX The specified index is invalid. I'm not sure what I'm doing wrong here. Thanks a head of time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top