【问题标题】:Writing and then Calling a C++ DLL from Excel VBA从 Excel VBA 编写然后调用 C++ DLL
【发布时间】:2015-04-08 02:04:49
【问题描述】:

我正在使用 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传入的文本值?

【问题讨论】:

    标签: c++ vba


    【解决方案1】:

    在我即将发布我的问题时想通了。答案是在VBA函数声明中将ByRef改为ByVal:

        Private Declare Function GetData Lib "getpdata.dll" (ByVal someText As String) As Integer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      相关资源
      最近更新 更多