我正在使用Visual Studio 2010在C ++中编写一个32位DLL,我打算从Excel VBA调用它。
C ++ cpp文件包含:
#include "windows.h"
#include "stdafx.h"
int _stdcall fnGetData (const LPCSTR someText) {
MessageBoxA(0, someText, "wizzle wuzzle", MB_OK | MB_ICONINFORMATION);
return 42;
}
虽然DEF文件有这个:
LIBRARY getpdata
EXPORTS
GetData=fnGetData
excel VBA是这样的:
Option Explicit
Private Declare Function GetData Lib "getpdata.dll" (ByRef someText As String) As Integer
Public Sub PassSomeText()
Dim ret As Integer, someText As String
someText = "Hello from my Excel Subroutine."
ret = GetData(someText)
End Sub
代码编译正常,并生成DLL。但是当我从Excel VBA调用DLL中的函数时,消息框中的文本是乱码:
我可能看到指针的文字表示?如何获取Excel VBA传入的文本值?
答案 0 :(得分:0)
就像我即将发布我的问题一样想出来。答案是在VBA函数声明中将ByRef更改为ByVal:
Private Declare Function GetData Lib "getpdata.dll" (ByVal someText As String) As Integer