我正在使用Windows 10(64位)和Excel 2016(32位),并希望在VBA中实现Yocto-4-20mA-Tx模块(https://www.yoctopuce.com/EN/products/usb-actuators/yocto-4-20ma-tx)的控制 .
根据文档,DLL是用C语言编写的,下面是三个基本功能 .
int yapiInitAPI(int connection_type, char *errmsg);
int yapiUpdateDeviceList(int forceupdate, char *errmsg);
int yapiHTTPRequest(char *device, char *request, char *buffer, int buffsize, int *fullsize, char *errmsg);
我已阅读(但可能未完全理解)以下内容:
我尝试在VBA模块中实现第一个函数(yapiInitAPI) . 使用DLL位于应用程序文件夹中
Option Explicit
Private Declare Function yapiInitAPI Lib "yapi" (ByVal connection_type As Long, ByVal errmsg As String) As Long
Sub myInit()
Dim nReturn As Long
Dim sError As String
nReturn = yapiInitAPI(1, sError)
MsgBox sError
MsgBox nReturn
End Sub
运行此结果
“运行时错误'49':错误的DLL调用约定”
在yapiInitAPI(1,sError)行 .
以下是一些问题:
-
所有DLL都可以在VBA中实现,还是必须遵循标准?
-
我认为"char *errmsg"我已经实现为"ByVal errmsg As String"是问题,如何将"char *"转换为VBA?
-
当我从yoctopuce下载DLL时,我得到32位和64位两个版本,我应该使用特定版本 . 如果是这样,它是依赖于我的操作系统还是Office版本?
-
我将DLL放在我的应用程序文件夹中(即存储.xlsm的地方)并且可以在VBA中使用"Lib 'yapi'"访问它,如果我把它放在Windows-System32中(并从应用程序文件夹中删除它)它会抱怨找不到文件 . 这不应该是一样的吗?
1 回答
该文档还提到“yapi.dll的每个入口点都是重复的 . 你会发现一个常规的C-decl版本和一个Visual Basic 6兼容版本,前缀为vb6_ . ” :-)
您必须为调用者应用程序和DLL使用相同的32/64位体系结构 .