我试图在VBA中使用C DLL函数,但是当它被调用时,Excel会崩溃。在VBA中,函数声明如下:
Public Declare Function HR8_CONNECT Lib "D:xxxxx.dll" _
(ByVal PortCom As Byte, ByVal Mode As Byte, ByVal Config As Byte, ByVal Dbg As Byte, ByVal context As String) As Byte
我有一个上下文参数的问题我认为这是一个指向unsigned char的指针。请参阅下一个C函数的标题
extern "C" __declspec(dllimport) UCHAR HR8_CONNECT(UCHAR, UCHAR, UCHAR, UCHAR, UCHAR*);
我尝试了许多用于传递指针的变体,例如byval string
,byref byte
等等......但Excel每次都会崩溃。
答案 0 :(得分:0)
您是否尝试为每个参数创建变量并在调用C函数后? 我实验或多或少类似的问题,它来自一个没有直接声明的指针发送的参数。
Dim paramPortCom As Byte;
Dim paramMode As Byte;
Dim paramConfig As Byte;
Dim paramDbg As Byte;
Dim paramContext As String;
Dim resultFn As Byte;
' Set value into each variable
paramPortCom := 123;
...
' Call C function
resultFn := HR8_CONNECT(paramPortCom, ...);