C
Caer_Dalben
Guest
I have defined a function in my c++ ActiveX object that fills and returns a safeArray of rank 3.
The length of the Array is not known in c# when calling the function.
In the idl of my c++ ActiveX object, I defined the function like so:
HRESULT Read ([in, out] SAFEARRAY(short)* ppBuffer, [out, retval] long* pReturn);
The c++ code defines an array of rank 3 and fills this array with my data:
STDMETHODIMP CIstFileAccess::Read(SAFEARRAY** ppBuffer, long* pReturn)
{
SAFEARRAYBOUND boundsArray[3]; // Three dimensions
...
// setup bounds
...
SAFEARRAY FAR* pMySafeArray = SafeArrayCreate(VT_I2, 3, boundsArray);
short* lpBuffer = NULL;
hrRetVal = SafeArrayAccessData(pMySafeArray, (void**)&lpBuffer);
...
// Fill lpBuffer with valid data
...
SafeArrayUnaccessData(pMySafeArray);
*ppBuffer = pMySafeArray;
*pReturn = 0;
}
Our old VB6 code called this function using a variable of type:
DIM buffer[] as integer
After calling our interface in VB6, the buffer was propperly filled with an array of rank 3.
In C# this function represents as:
int interfaceClass.Read ( ref short[] ppBuffer );
calling it like so:
short[] buffer = null;
int ret = interfaceClass.Read ( ref ppBuffer );
throws an exception:
"A SafeArray of rank 3 was passed to a method, that expects an array of rank 1"
So my question:
What do I have to do, to be able to call this function propperly from C# ?
Continue reading...
The length of the Array is not known in c# when calling the function.
In the idl of my c++ ActiveX object, I defined the function like so:
HRESULT Read ([in, out] SAFEARRAY(short)* ppBuffer, [out, retval] long* pReturn);
The c++ code defines an array of rank 3 and fills this array with my data:
STDMETHODIMP CIstFileAccess::Read(SAFEARRAY** ppBuffer, long* pReturn)
{
SAFEARRAYBOUND boundsArray[3]; // Three dimensions
...
// setup bounds
...
SAFEARRAY FAR* pMySafeArray = SafeArrayCreate(VT_I2, 3, boundsArray);
short* lpBuffer = NULL;
hrRetVal = SafeArrayAccessData(pMySafeArray, (void**)&lpBuffer);
...
// Fill lpBuffer with valid data
...
SafeArrayUnaccessData(pMySafeArray);
*ppBuffer = pMySafeArray;
*pReturn = 0;
}
Our old VB6 code called this function using a variable of type:
DIM buffer[] as integer
After calling our interface in VB6, the buffer was propperly filled with an array of rank 3.
In C# this function represents as:
int interfaceClass.Read ( ref short[] ppBuffer );
calling it like so:
short[] buffer = null;
int ret = interfaceClass.Read ( ref ppBuffer );
throws an exception:
"A SafeArray of rank 3 was passed to a method, that expects an array of rank 1"
So my question:
What do I have to do, to be able to call this function propperly from C# ?
Continue reading...