+ Reply to Thread
Results 1 to 11 of 11

How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Now, I'm currently trying to receive the multiple return value from DLL in VBA(Excel Visual Basic).

    This is my DLL code.

            double _stdcall pll_dll(double* datain0, double* datain1, double* dataout0, double* dataout1)
        {
        	dataout0[0] = datain0[0]+10;
        	dataout1[0] = datain1[0]+10;
        
        	return 0;
        }
    Then I want to give some value to the DLL by using the datain0 and datain1 and receive the dataout0 and dataout1 from the DLL in the VBA(Excel Visual Basic).

    This is my VBA code.actually, I'm not familiar with VBA(Excel Visual Basic)

    
        Option Explicit
        
        
        Private Declare PtrSafe Function pll_dll Lib "F:\work\pll_dll\x64\Debug\pll_dll.dll" (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double) As Double
        Dim Error As Integer
        Dim dat0 As Double
        Dim dat1 As Double
        Dim dat2 As Double
        Dim dat3 As Double
        
          
        Function pll_dll_excel(data0 As Double, data1 As Double, data2 As Double, data3 As Double) As Double
        
              pll_dll_excel = pll_dll(data0, data1, data2, data3)
        End Function
        
        
        Sub useSquareInVBA()
           MsgBox pll_dll_excel(3, 4, Cells(5, 5), Cells(6, 6))
        End Sub
    What am I supposed to make the function which is what I want to give datain0=3, datain1=4 and receive the dataout0 and dataout1 values?
    If I run the above code in the excel, I've got a problem halt
    Last edited by u24c02; 03-10-2017 at 08:50 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Your dll function is modifying the variables passed to it using ByRef (data2 and data3) so your function pll_dll_excel should return those values as an array instead of passing the returned 0 from the dll. This is how you should be using the ByRef variables - I have just created a fake pll_dll function for demonstration.... when you pass Cells() you are only passing the value and not the cell object so you cannot write directly to it.

    Option Explicit
    Function pll_dll(ByRef datain0 As Double, ByRef datain1 As Double, ByRef dataout0 As Double, ByRef dataout1 As Double) As Boolean
        dataout0 = datain0 + 10
        dataout1 = datain1 + 10
        pll_dll = True
    End Function
    
    Function pll_dll_excel(data0 As Double, data1 As Double) As Variant
        Dim data2 As Double
        Dim data3 As Double
        Dim ret As Boolean
        
        ret = pll_dll(data0, data1, data2, data3)
        
        If ret Then
            pll_dll_excel = Array(data2, data3)
        Else
            pll_dll_excel = xlErrNull
        End If
    End Function
    
    
    Sub useSquareInVBA()
        Dim v As Variant
        
        v = pll_dll_excel(3, 4)
        
        If Not v = xlErrNull Then
            Cells(5, 5).Value = v(0)
            Cells(6, 6).Value = v(1)
        Else
            MsgBox "oops"
        End If
        
    End Sub
    Last edited by Bernie Deitrick; 03-10-2017 at 01:03 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29
    Quote Originally Posted by Bernie Deitrick View Post
    Your dll function is modifying the variables passed to it using ByRef (data2 and data3) so your function pll_dll_excel should return those values as an array instead of passing the returned 0 from the dll. This is how you should be using the ByRef variables - I have just created a fake pll_dll function for demonstration.... when you pass Cells() you are only passing the value and not the cell object so you cannot write directly to it.

    [CODE]Option Explicit
    Function pll_dll(ByRef datain0 As Double, ByRef datain1 As Double, ByRef dataout0 As Double, ByRef dataout1 As Double) As Boolean
    dataout0 = datain0 + 10
    dataout1 = datain1 + 10
    pll_dll = True
    End Function

    Function pll_dll_excel(data0 As Double, data1 As Double) As Variant
    Dim data2 As Double
    Dim data3 As Double
    Dim ret As Boolean

    ret = pll_dll(data0, data1, data2, data3)

    If ret Then
    pll_dll_excel = Array(data2, data3)
    Else
    pll_dll_excel = xlErrNull
    End If
    End Function


    Sub useSquareInVBA()
    Dim v As Variant

    v = pll_dll_excel(3, 4)

    If Not v = xlErrNull Then
    Cells(5, 5).Value = v(0)
    Cells(6, 6).Value = v(1)
    Else
    MsgBox "oops"
    End If

    End Sub
    [JCODE]

    Dear bernie,

    I've just modified as the below.

    
    Function pll_dll_excel(data1 As Double, data2 As Double,   data3 As Double, data4 As Double) As Double 
     pll_dll_excel = pll_dll(data1, data2, data3, data4) 
    End
    
    Function Sub useSquareInVBA() 
     MsgBox pll_dll_excel(3, 4, d1, d2)
     Cells(5, 5).Value = d1
     Cells(6, 6).Value = d2 
    End Sub
    Is this correct?
    Last edited by u24c02; 03-11-2017 at 12:04 PM.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    No.

    You cannot have a Function Sub ("Function Sub useSquareInVBA() ")

    Try just using the dll function directly - your dll already uses ByRef:

    Function Sub useSquareInVBA() 
       Dim v As Variant
       v = pll_dll(3, 4, d1, d2)
       Cells(5, 5).Value = d1
       Cells(6, 6).Value = d2 
    End Sub

  5. #5
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Quote Originally Posted by Bernie Deitrick View Post
    No.

    You cannot have a Function Sub ("Function Sub useSquareInVBA() ")

    Try just using the dll function directly - your dll already uses ByRef:

    Function Sub useSquareInVBA() 
       Dim v As Variant
       v = pll_dll(3, 4, d1, d2)
       Cells(5, 5).Value = d1
       Cells(6, 6).Value = d2 
    End Sub


    Dear Bernie,
    your answer work as well,
    So I've just added the one more parameter in the C DLL module as the below,

    double _stdcall pll_dll(double* datain0, double* datain1, double* dataout0, double* dataout1, char * str)
    {
    	dataout0[0] = datain0[0] + 10;
    	dataout1[0] = datain1[0] + 10;
    	str[0] = 'c';
    
    	return 0;
    }
    and the below is the EXCEL VBA code, I've just added "char * str" but in this case,
    I've got program halt when I run.

    double _stdcall pll_dll(double* datain0, double* datain1, double* dataout0, double* dataout1, char * str)
    {
    	dataout0[0] = datain0[0] + 10;
    	dataout1[0] = datain1[0] + 10;
    	str[0] = 'c';
    
    	return 0;
    }
    how can I get the string and char type data from dll?

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Have you updated your declaration line?

        Private Declare PtrSafe Function pll_dll Lib "F:\work\pll_dll\x64\Debug\pll_dll.dll" (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double) As Double

  7. #7
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Quote Originally Posted by Bernie Deitrick View Post
    Have you updated your declaration line?

        Private Declare PtrSafe Function pll_dll Lib "F:\work\pll_dll\x64\Debug\pll_dll.dll" (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double) As Double
    Dear Bernie,
    This is my C code
    q8.JPG

    ans the below code is the VBA code


    Option Explicit
    
    Private Declare PtrSafe Function pll_dll Lib "C:\Users\carter\Downloads\pll_dll\pll_dll\x64\Debug\pll_dll.dll" _
    (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByVal str As String) As Double
    
    Dim Error As Integer
    
    Dim d1 As Double
    Dim d2 As Double
    Dim d3 As Double
    Dim d4 As Double
    Dim sometext As String
    Dim str As String
    
    
    Function pll_dll_excel(data1 As Double, data2 As Double, data3 As Double, data4 As Double, ByRef text As String) As Double
    
    pll_dll_excel = pll_dll(data1, data2, data3, data4, text)
        
    End Function
    
    
    
    
    
    
    Sub useSquareInVBA()
        MsgBox pll_dll_excel(3, 4, d1, d2, sometext)
        Cells(5, 5).Value = d1
        Cells(6, 6).Value = d2
        Cells(7, 7).Value = sometext
    End Sub

    The problem is that I can get the any string data from dll.
    Would you please help me please?
    Last edited by u24c02; 03-13-2017 at 11:28 AM.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Change

     ByVal str As String
    
    Private Declare PtrSafe Function pll_dll Lib "C:\Users\carter\Downloads\pll_dll\pll_dll\x64\Debug\pll_dll.dll" _
    (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByVal str As String) As Double
    to

     ByRef str As String
    
    Private Declare PtrSafe Function pll_dll Lib "C:\Users\carter\Downloads\pll_dll\pll_dll\x64\Debug\pll_dll.dll" _
    (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByRef str As String) As Double

  9. #9
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29
    Quote Originally Posted by Bernie Deitrick View Post
    Change

     ByVal str As String
    
    Private Declare PtrSafe Function pll_dll Lib "C:\Users\carter\Downloads\pll_dll\pll_dll\x64\Debug\pll_dll.dll" _
    (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByVal str As String) As Double
    to

     ByRef str As String
    
    Private Declare PtrSafe Function pll_dll Lib "C:\Users\carter\Downloads\pll_dll\pll_dll\x64\Debug\pll_dll.dll" _
    (ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByRef str As String) As Double


    Thanks, but both the same result.

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    It might be an issue with C, but I don't know C - sorry. You might want to start a new thread.

  11. #11
    Registered User
    Join Date
    03-10-2017
    Location
    London
    MS-Off Ver
    2016
    Posts
    29

    Re: How can I receive the multi return values from DLL in VBA(Excel Visual Basic)?

    Quote Originally Posted by Bernie Deitrick View Post
    It might be an issue with C, but I don't know C - sorry. You might want to start a new thread.
    Thanks Bernie, I got it

+ 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. Excel Macro - Visual Basic - Send command to serial port - Receive Data.
    By reiiling in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-08-2017, 09:37 AM
  2. [SOLVED] Can I run Visual Basic procedure using Excel Visual Basic editor?
    By john.jacobs71 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-26-2005, 10:25 AM
  3. Excel in Visual Basic
    By Spike in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-29-2005, 02:15 PM
  4. Visual Basic Multi-Stepped process
    By MK@Hartford in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2005, 01:35 PM
  5. Replies: 1
    Last Post: 09-13-2005, 07:06 AM
  6. converting formulas into values using a macro in visual basic edit
    By F.C in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2005, 05:05 AM
  7. visual basic in excel -
    By DKehl in forum Excel General
    Replies: 2
    Last Post: 03-23-2005, 04:06 PM

Tags for this Thread

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