从VBA调用GetProcAddress始终返回null [英] Calling GetProcAddress from VBA always returns null

查看:77
本文介绍了从VBA调用GetProcAddress始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有64位Windows 10和MS Office 64位.我正在尝试让VBA for Powerpoint在自写的64位Windows DLL中加载并执行功能.为了防止出口名称篡改,我使用了外部C:

I have 64 bit windows 10 with MS Office 64 bit. I am trying to get the VBA for Powerpoint to load and execute a function in a self-written 64 bit windows DLL. To prevent export name mangling I have used extern C:

extern "C" {
    __declspec(dllexport) long jaadd(long a, long b)
    {
        return a + b;
    }
}

C ++模块可以调用此简单函数,而不会出现没有问题:

This simple function can be called by a C++ module with no problems:

    hinstDLL = LoadLibrary(L"D:\\Visual Studio 2017\\Projects\\PopUpDLL\\x64\\Debug\\PopUpDLL.dll");
    if (hinstDLL != NULL)
    {
        jaadd = (AddFunc)GetProcAddress(hinstDLL, "jaadd");
        if (jaadd != NULL) {
            result = jaadd(13, 40);
        }       
        fFreeDLL = FreeLibrary(hinstDLL);
    }

在Powerpoint中尝试从VBA调用DLL时出现问题.GetProcAddress始终返回零,因此FreeLibrary也会返回

The problem arises when trying to call the DLL from VBA in Powerpoint. GetProcAddress always returns zero and so does FreeLibrary

    Private Declare PtrSafe Function FreeLibrary Lib "kernel32" (ByVal      hLibModule As Long) As LongLong
    Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Private hLib As Long
    Sub LLib()
        hLib = LoadLibrary("D:\\Visual Studio 2017\\Projects\\PopUpDLL\\x64\\Debug\\PopUpDLL.dll")
        MsgBox hLib
        Dim pprocaddress As Long
        pprocaddress = GetProcAddress(hLib, "jaadd")  ***** always returns 0
        MsgBox pprocaddress
        xx = FreeLibrary(hLib)  ***** always returns 0
        MsgBox xx
    End Sub

非常感谢您的帮助.

推荐答案

您尽一切努力

Private Declare PtrSafe Function jaadd Lib "......\x64\Debug\PopUpDLL.dll" 
Alias "_jaadd@8" (ByVal arg1 As Long, ByVal arg2 as Long) As Long

请注意别名处理的算法":_ +您的原始名称+ @ +参数字节的总和.

Note the Alias mangling "algorithm": _ + your original name + @ + sum of argument bytes.

您有两个VBA Long,或两个C#整数,即4 + 4 = 8.

You have two VBA Longs, or two C# ints, 4 + 4 = 8.

您也可以在VBA-land中省去twin \东西.

You can also dispense with the twin \ thing here in VBA-land.

另请参见 https://docs.microsoft.com/zh-CN/office/client-developer/excel/how-to-access-dlls-in-excel

最后,请确保对32位VBA使用32位DLL,对于64位VBA使用64位DLL.

Lastly, make sure you use a 32-bit DLL for 32-bit VBA, and a 64-bit DLL for 64-bit VBA.

这么多的网站在其DECLARE语句中都有LoadLibrary的句柄,并且将GetProcAddress返回为Long而不是LongPtr.

So many websites in their DECLARE statements have the handle for LoadLibrary and return of GetProcAddress as Long instead of LongPtr.

问题在于信息过时了-代码从未更新过以反映Excel 2009以后的VBA状态.

Problem is that the information is stale - the code was never updated to reflect the post-Excel 2009 state of VBA.

这篇关于从VBA调用GetProcAddress始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆