我目前正在尝试从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;
}
然后我想使用datain0
和datain1
为DLL赋予一些价值,并从DLL接收dataout0
和dataout1
到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=3
,datain1=4
并接收dataout0
和dataout1
值?
我有一些线索。我已经更新了我的代码。
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接收结果值是否正确?
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
这是对的吗?
答案 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
的函数。现在你要传递四个数字。它会将结果放入d1
和d2
。