VBA Argument pass to DLL Issue RRS feed

  • Question

  • Hello all,

    So I've come across the strangest thing.  I have a DLL that generates some data that it returns to Excel for display.  The C++/DLL code works perfectly.  The VBA works fine UNTIL it passes the arguments to the DLL.  It passes all the arguments fine Except for the last one.  For that, no matter what I tell it to pass, it passes some huge number.

    Additionally, I added another argument, the 7th, just to test.  When I pass the same value as both the 6th and the 7th argument, it does the same thing - huge value for the 6th and the correct value for the 7th.  For some reason the 6th argument is being passed incorrectly and for the life of me I don't know why.

    VBA Code:

    Option Explicit
    ' Declare the LMM Function that's in the DLL
    Declare PtrSafe Function GenCudaLMMPaths Lib "C:\Path to DLL\LMMExcel.dll" Alias "GenerateCUDALMMPaths" (xTimes#, xRates#, xVols#, xRData#, ByRef ArrLen As Long, ByRef NumPaths As Long, ByRef Dummy As Long) As Long
    ' Generate LMM Paths on Click
    Sub LMM_Click()
        Dim Times#(), Rates#(), Vols#()
        Dim x As Long
        Dim y As Long
        Dim rTimes As Range
        Dim rRates As Range
        Dim rVols As Range
        Dim cell As Range
        Dim sz As Long
        sz = 15
        ' Resize
        ReDim Times(sz), Rates(sz), Vols(sz)
        ' Fill in Data
        Set rTimes = Sheets("Market").Range("C2:Q2")
        x = 1
        For Each cell In rTimes
            Times(x) = cell.Value
            x = x + 1
        Set rRates = Sheets("Market").Range("C5:Q5")
        x = 1
        For Each cell In rRates
            Rates(x) = cell.Value
            x = x + 1
        Set rVols = Sheets("Market").Range("C4:Q4")
        x = 1
        For Each cell In rVols
            Vols(x) = cell.Value / 10000
            x = x + 1
        'Call the Function
        Dim np As Long
        np = Sheets("LMM").Range("C2").Value
        Dim useCuda As Boolean
        If Sheets("LMM").Range("C3").Value = "GPU" Then
            useCuda = True
            useCuda = False
        End If
        Dim rData#()
        Dim rValue
        ReDim rData(np * sz * (sz + 3))
        rValue = GenCudaLMMPaths(Times(1), Rates(1), Vols(1), rData(1), sz, np, np)
        If rValue = -1 Then
            'No CUDA Card
            MsgBox ("Your system doesn't have a CUDA Enabled GPU")
        ElseIf rValue = 1 Then
            'Error Occurred
            MsgBox ("An error occurred while trying to generate LMM paths")
        ElseIf rValue = 0 Then
            ' Need to reformat return data
            Dim fmtData()
            ReDim fmtData(np * sz, sz)
            Dim i, j, k
            For i = 0 To np - 1
                For j = 0 To np - 1
                    For k = 0 To np - 1
                        fmtData(((i * sz) + j) + 1, k + 1) = rData(((i * sz * sz) + (j * sz) + k) + 1)
                    Next k
                Next j
            Next i
            'Fill in data
            Sheets("LMM").Range("A8:K" & (np * sz)) = fmtData
            'Too many requested paths for this CUDA card
            MsgBox ("In order to prevent GPU Lock-up, you cannot request more than " & rValue & " paths.")
            Sheets("LMM").Range("C2").Value = rValue
        End If
    End Sub

    C++ DLL Function Declaration:

    int __stdcall GenerateCUDALMMPaths(double* arrTimes, double* arrRates, double* arrVols, double* retData, int& ArrLength, int& NPaths, int& Dummy);

    Now I could just bite the bullet and skip the 6th variable, but that's not an elegant solution and at this point I really want to know what's going on here.


    Thursday, March 27, 2014 7:44 PM

All replies

  • Hi,

    Based on your code, the variable “np” is defined with the value of Range ("C2") in Worksheet "LMM". I cannot find any issue in the VBA code when calling the GenCudaLMMPaths function with arguments. As you said, the 7th argument is correct, so it means the values passing to the GenCudaLMMPaths function are all correct since you pass the same 6th and 7th value.

    So I think the VBA code is correct and the issue is more related to the code in C++ function GenerateCUDALMMPaths. To test it, I suggest you adding code to print out all received arguments at the start of the C++ function GenerateCUDALMMPaths to check the values. If they are all correct, it means the issue is caused by the programming in the C++ function GenerateCUDALMMPaths and you need to check the C++ code.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 28, 2014 9:07 AM
  • I have done debugging in VS2012 and checked the passed in variables.  If I could post links I could show screenshots from the debug.  The 6th argument is incorrect, the 7th is correct.  The issue seems to be going on with the passing from the VBA to the DLL.  I don't know if it's an excel issue or what but all my code is correct.

    Friday, March 28, 2014 4:17 PM
  • Hi,

    I failed to reproduce your issue. But based on your code, I find the types of parameters in VBA are not matched to the type of parameters in the C++ function. In addition, the first parameter in the C++ function is a double pointer, but you pass a string in the VBA code. So I suggest you editing your code to match all the parameters to see whether the issue is resolved.

    By the way, since the issue is more related to the feature of VBA, I'd like to move it to VBA forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thanks for your understanding.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 31, 2014 10:09 AM
  • I can't add to what Luna Zhang has already suggested, I don't think it's an Excel issue.

    Just one observation though, in your VBA declaration of the dll you declared the last three variables ByRef which indeed match your dll. However you've also implicitly declared the first four as ByRef though it looks like your dll expects ByVal. Note in VBA ByRef is default. I can't think why that might contribute to your problem though.

    Monday, March 31, 2014 2:30 PM