如何从VBA(Excel Visual Basic)中的DLL接收多返回值?

时间:2017-03-10 12:03:33

标签: excel-vba dll vba excel

我目前正在尝试从DLL接收多个返回值到Excel VBA。

这是我的DLL代码。

double _stdcall pll_dll(double* datain0, double* datain1, double* dataout0, double* dataout1)
{
    dataout0[0] = datain0[0]+10;
    dataout1[0] = datain1[0]+10;

    return 0;
}

然后我想使用datain0datain1为DLL赋予一些价值,并从DLL接收dataout0dataout1到VBA。

这是我的VBA代码。实际上,我不熟悉Excel VBA。

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

我如何制作我想要提供的功能datain0=3datain1=4并接收dataout0dataout1值?

修改。

我有一些线索。我已经更新了我的代码。

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
Dim d1 As Double
Dim d2 As Double


Function pll_dll_excel(data2 As Double, data3 As Double) As Double
    pll_dll_excel = pll_dll(3, 4, data2, data3)
    Cells(5, 5).Value = d1
    Cells(6, 6).Value = d2
End Function

Sub useSquareInVBA() ' Here is the start 
    MsgBox pll_dll_excel(d1, d2)
End Sub

从DLL接收结果值是否正确?

EDIT2

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

这是对的吗?

1 个答案:

答案 0 :(得分:1)

dim d1 as double, d2 as double

pll_dll 3, 4, d1, d2

cells(5,5).value = d1
cells(6,6).value = d2

您正确地Declare了一个接受四个Double s ByRef的函数。现在你要传递四个数字。它会将结果放入d1d2