Passing array from VB to DLL and returning array 
Author Message
 Passing array from VB to DLL and returning array

    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 -



Fri, 22 Dec 2000 03:00:00 GMT  
 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 -



Fri, 22 Dec 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing an array (or array of a structure) from VB to VC++

2. passing arrays from vb dll to vb form

3. Returning an array from DLL to VB

4. Returning an array from a VB dll

5. VB calling C++ DLL returning an array??

6. PB/DLL passing multidimensional VB arrays by pointers

7. Passing Array of data from DLL to VB

8. passing (string) arrays from VB to C-DLL and vice versa

9. Passing Multi-Dimension Arrays from VB to DLL

10. Pass array from ASP to VB ActiveX DLL

11. Passing array of strings to a dll in VB

12. Passing a binary array from VB to a DLL

 

 
Powered by phpBB® Forum Software