【发布时间】:2018-09-04 07:44:43
【问题描述】:
我使用的是 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 中实现还是必须遵循标准?
- 我认为我实现为“ByVal errmsg As String”的“char *errmsg”是个问题,应该如何在 VBA 中将“char *”翻译成?
- 当我从 yoctopuce 下载 DLL 时,我得到了 32 位和 64 位的两个版本,我应该使用一个特定的版本吗?如果是这样,这取决于我的操作系统还是 Office 版本?
- 我将 DLL 放在我的应用程序文件夹中(即存储 .xlsm 的位置),如果我将它放在 Windows-System32 中(并从应用程序文件夹中删除它),则可以在 VBA 中使用“Lib 'yapi'”访问它抱怨找不到文件。这不应该是一样的吗?
【问题讨论】: