尝试从 VBA (MS Access) 调用 C++ DLL,并不断收到“错误的 dll 调用约定”错误,以及 MS Access 崩溃。
这是我尝试调用的 C++ API 函数:
_IMPORT HRESULT _CONVENTION PCRSNewTrip (Trip *pTripID);
“旅行”定义为:
typedef long Trip;
来自 API header :
#if defined (__BORLANDC__)
#define _IMPORT __declspec( dllimport )
#define _CONVENTION __stdcall
#elif defined (_MSC_VER)
#define _IMPORT _declspec( dllimport )
#define _CONVENTION _cdecl
#endif
这里有一些关于该函数的信息: PCRSNewTrip() 在传入的指针参数 (tripID) 中放置新行程的句柄。返回代码与所有其他 DLL 函数(用于错误处理)相同。
这是我最近尝试调用该函数:
Public Declare Function PCRSNewTrip Lib "C:\xxx\pcrsrv32.dll" Alias "_PCRSNewTrip" (ByRef myTripPtr As Long) As Long
Private Sub NewTrip_Click()
Dim myTrip As Long
Dim myTripPtr As Long
myTripPtr = VarPtr(myTrip)
myTrip = PCRSNewTrip(myTripPtr)
EndSub
我收到“Bad DLL calling convention”错误。
最佳答案
猜测这是条件编译头代码的“实时”分支:
#elif defined (_MSC_VER)
#define _IMPORT _declspec( dllimport )
#define _CONVENTION _cdecl
#endif
Windows 上的 VBA 不支持 _cdecl
。您需要使用使用 __stdcall
的该 DLL 的构建。
关于c++ - 使用指针调用 C++ DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099021/