
Passing array from VB to DLL and returning array
You can download DemoDll.zip from the FAQ page on my web
site if you need a template. Otherwise, this code demonstrates
how to create an array in VB, pass it to a VC DLL, and modify
the array elements and update the array for use by VB.
' VB code
Private Declare Function IncrementAndAverageArray Lib "DemoDLL.dll" _
(intArrayOfInts() As Integer, dbReturn As Double) As Double
Public Sub AverageOfIncrementedElements()
Dim dblTotal As Double
Dim iTempCounter As Integer
Dim nArray(10) As Integer
' Fill the array with values from the counter.
For iTempCounter = 0 To 9
nArray(iTempCounter) = iTempCounter
Next iTempCounter
' Print the avarage out in a messagebox
MsgBox "The total average is " & _
IncrementAndAverageArray(nArray(), dblTotal), vbInformation,
DEMO_DLL_TEST
End Sub
// VC code
double _stdcall IncrementAndAverageArray(LPSAFEARRAY *ppsafearray, double
*dblResult)
{
short sElem;
long larrayLb, larrayUb, l;
double dResult;
if ((*ppsafearray)->cDims != 1) // If array has no elements
return DEMO_ERROR; // return error code
if (FAILED(SafeArrayGetLBound(*ppsafearray, 1, &larrayLb)) ||
FAILED(SafeArrayGetUBound(*ppsafearray, 1, &larrayUb)))
return DEMO_ERROR; // return error code
// Sum the elements
for (l = larrayLb, dResult = 0; l <= larrayUb; l++)
{
if (FAILED(SafeArrayGetElement
(*ppsafearray, &l, &sElem)))
return DEMO_ERROR;
// Add 3 to each array element prior to summing
sElem += 3;
// Store the element back into the array
SafeArrayPutElement(*ppsafearray, &l, &sElem);
dResult += sElem;
}
//return average by dividing number of elements
*dblResult = dResult/larrayUb;
return *dblResult;
Quote:
}
--
Michael D. Long
http://www.geocities.com/siliconvalley/heights/9748/
Quote:
> I am having trouble writing a VB5 front end to pass an array from
> VB5 into a C++ DLL and performing a simple operation on it (say adding
> 3) to each element and return the result back into an array in VB5 (the
> same array if possible). Then I would like to print this to a text
> box. I have been able to accomplish this by writing the modified array
> striaght from the modified DLL array into a VB5 string but cannot figure
> out how to get it back into a VB5 array. Any help or hints is
> appreciated. Please EMAIL if possible. Thanks.
> - James - JC -