When you use SAFEARRAY as a parameter, VB passes you an existing SAFEARRAY pointer. CA_Array1DToSafeArray creates a new safearray that VB does not know about, which is why you were not able to pass the data back. You can use the SafeArray functions that are part of the Windows SDK to copy our data to the SafeArray. Make sure to add oleaut32.lib to your CVI project.
int DLLEXPORT dspReturnPixels (SAFEARRAY **Spel_mean)
{
int pel_mean[21504];
short int i;
long lElements; // number of elements in the array
VARTYPE vt;
int* pdata;
for (i = 0; i < 21504; i++)
{
pel_mean[i] = i;
}
// checking if it is an one-dimensional array
if(SafeArrayGetDim(*Spel_mean) != 1)
return 1;
SafeArrayGetVartype(*Spel_mean,&vt);
if(vt != VT_I4) return 1;
// how many elements are there in the array
if( (*Spel_mean)->rgsabound[0].cElements != 21504) return 1;
// locking the array before using its elements
SafeArrayLock(*Spel_mean);
SafeArrayAccessData(*Spel_mean, (void HUGEP**)&pdata);
//copying the data
memcpy(pdata,pel_mean,21504*sizeof(int));
// releasing the array
SafeArrayUnlock(*Spel_mean);
return (4);
}
And you need to change your VB declaration to the following to reflect the return type.
Private Declare Function dspReturnPixels Lib "test.dll" (x1() As Long)
As LongRefer to the
MSDN for more information about the safearray functions.
The following document provides you with some alternatives if you do not want to use SafeArrays. The pointer approach is the simplest.
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q207931
Hope this helps
Bilal Durrani
NI