+ Reply to Thread
Results 1 to 6 of 6

VBA Argument pass to DLL Issue

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    3

    VBA Argument pass to DLL Issue

    Hello,

    So I'm completely new to VBA and I've written a DLL to do some functions and the idea is to pass the results back to Excel. The DLL works fine, I've tested it extensively. The issue is the last argument I pass to it from VBA in Excel, no matter what value it is, always gets passed as the same, very large number. Below is the VBA code and then the DLL function declaration. Help would be greatly appreciated I have no clue what's going on here. The last two arguments are to be passed as number values, both are passed in the same way, and the first works, the second doesn't.

    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#, ByVal ArrLen&, ByVal NPaths&) 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&
        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
        Next
                  
        Set rRates = Sheets("Market").Range("C5:Q5")
        
        x = 1
        For Each cell In rRates
            Rates(x) = cell.Value
            x = x + 1
        Next
            
        Set rVols = Sheets("Market").Range("C4:Q4")
        
        x = 1
        For Each cell In rVols
            Vols(x) = cell.Value / 10000
            x = x + 1
        Next
        
        'Call the Function
        Dim np&
        
        np = Sheets("LMM").Range("C2").Value
        
        Dim useCuda As Boolean
        
        If Sheets("LMM").Range("C3").Value = "GPU" Then
            useCuda = True
        Else
            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)
        
        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
            'Success
            ' 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
        Else
            '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
    And here's the function definition from the .h file for the DLL:
    int __stdcall GenerateCUDALMMPaths(double* arrTimes, double* arrRates, double* arrVols, double* retData, int ArrLen, int NPaths);
    Thanks!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: VBA Argument pass to DLL Issue

    Have you checked what the value is in C2 on the sheet 'LMM'?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-27-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: VBA Argument pass to DLL Issue

    Yes, I can MsgBox(np) and it gives the correct value, but when I'm debugging and check the value once it's passed into the DLL it's completely different. What's weird is if I switch the ordering - pass in the np where the sz is and vice-versa, then the np is passed in fine but the sz is that same large number, which is really baffling.

  4. #4
    Registered User
    Join Date
    03-27-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: VBA Argument pass to DLL Issue

    Here's what's really weird. I tacked on another variable for the DLL function as a test/dummy variable. Then I pass np in both, in the first pass it gives the same large value - in the second the correct value. For some reason there's something off with the 6th argument, just that one.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Argument pass to DLL Issue

    can you register the .dll and set a reference to it and then call it without the declare? does it work then?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    03-27-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: VBA Argument pass to DLL Issue

    Well the DLL isn't designed to be registered / doesn't have the proper register/unregister functions but I can try. In the meantime does anyone else have any ideas?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. pass control as argument
    By horbay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-17-2011, 09:04 AM
  2. Pass argument to SetSourceData
    By khdani in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 10-22-2008, 07:54 AM
  3. Pass function as argument to UDF
    By Ron Rosenfeld in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-09-2006, 08:55 AM
  4. Pass Argument?
    By Hal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-05-2005, 01:20 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1