Results 1 to 2 of 2

Thread: [Excel VBA] C DLL function returning a 2D array

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    1

    [Excel VBA] C DLL function returning a 2D array

    Hi,

    I've made a DLL from a program originally written in C, in order to call it from a Excel VBA macro.
    One of the functions I would like to be able to use from Excel returns a 2D array in the form of a double pointer (double**). The prototype of this function is right below:
    Code:
    __declspec (dllexport) double** __stdcall calculMachCarreRegime(double S2, double Sjonction, double Lconvergent, double Ljonction, double alpha, double beta, double delta, double dx, double k, double f, double xCritique, int nPoints, int regime);
    I can successfully call this function from my Excel macro, but I can't get it to work properly, ie. the array returned by this function isn't stored in an array in the macro when I use the following instructions:
    Code:
    Declare Function calculMachCarreRegime Lib "E:\dropbox\be\4- programmes\vba-excel\dll-cas2\cas2.dll" (ByVal S2 As Double, ByVal Sjonction As Double, ByVal Lconvergent As Double, ByVal Ljonction As Double, ByVal alpha As Double, ByVal beta As Double, ByVal delta As Double, ByVal dx As Double, ByVal k As Double, ByVal f As Double, ByVal xCritique As Double, ByVal nPoints As Long, ByVal regime As Integer) As Double()
    
    Dim MachCarre() As Double
    'Computation of array size nPoints
    ReDim MachCarre(nPoints - 1, 1)
    MachCarre = calculMachCarreRegime(S2, Sjonction, Lconvergent, Ljonction, alpha, beta, delta, dx, k, f, xCritique, nPoints, 2)
    I've already tried several combinations, such as using a Variant to store the result, with no luck so far. I always get errors such as no. 9 (Subscript out of range), when trying to access one of the cells - for instance MachCarre(0, 0) - in the array.

    Do you have any suggestions on the best way to proceed in order to get this to work? Thanks in advance.

    Regards,

    Frédéric

  2. #2
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: [Excel VBA] C DLL function returning a 2D array

    Arrays don't appear to be supported as a return type. The api in the c program would have to be modified to accommodate the return array as an argument.

    type Optional. Data type of the value returned by a Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (variable length only), or Variant, a user-defined type, or an object type.

    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width