如何将变量传递给 DLL 导出的函数,出现错误 LNK2001

发布于 2024-11-29 07:08:47 字数 2305 浏览 1 评论 0原文

我必须将 HWND 变量从主程序传递到 DLL 导出的函数。变量称为 mainHwnd,DLL 的定义方式如下:

mydll.h

#ifdef MYDLL_EXPORTS
#define MYDLL_API extern "C" __declspec(dllexport)
#else
#define MYDLL_API extern "C" __declspec(dllimport)
#endif

MYDLL_API HWND mainHwnd;
MYDLL_API void testFunction(void);
MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam);

mydll.cpp

#include "stdafx.h"
#include "mydll.h"
#include <string>

#define CLASSNAMELEN    5

MYDLL_API HWND mainHwnd = 0;

// This is an example of an exported function.
MYDLL_API void testFunction(void)
{
MessageBox(NULL, (LPCWSTR)L"Test", (LPCWSTR)L"Test", MB_OK);
}

MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
    if(wParam != NULL && (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP))
    {
        std::wstring s;
        MessageBox(NULL, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        GetClassName(m->hwnd, (LPWSTR) s.c_str(), CLASSNAMELEN);
        if(s == L"Edit")
            SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam);
    }
}

// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

主程序

MYDLL_API HWND mainHwnd;
...
case WM_CREATE:
    {   
        // now it will load DLL and set up hook procedure for mouse events

        // declares local variables
        HOOKPROC hkprcMouseProc;
        HINSTANCE hinstDLL; 
        HHOOK hhookMouseProc;
        // loads DLL
        if((hinstDLL = LoadLibrary(TEXT("C:\\Users\\Francesco\\Dropbox\\poli\\bi\\not\\pds\\sp\\wk5\\lsp5\\Debug\\mydll.dll"))) == NULL)
        {
            MessageBox(hWnd, (LPCWSTR)L"Error loading DLL", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR);
            break;
        }

        // saves main window handle for DLL functions
        mainHwnd = hWnd;
...

编译我

error LNK2001: unresolved external symbol __imp__mainHwnd

在使用 dumpbin /exports mydll 时 得到.dll 我发现全局变量名称被破坏为:

mainHwnd = _mainHwnd

我在谷歌上浏览了很多页面,但没有结果。也许有概念错误.. 谢谢

I have to pass a HWND variable from the main program to a function exported by a DLL. Variable is called mainHwnd and DLL is defined in this way:

mydll.h

#ifdef MYDLL_EXPORTS
#define MYDLL_API extern "C" __declspec(dllexport)
#else
#define MYDLL_API extern "C" __declspec(dllimport)
#endif

MYDLL_API HWND mainHwnd;
MYDLL_API void testFunction(void);
MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam);

mydll.cpp

#include "stdafx.h"
#include "mydll.h"
#include <string>

#define CLASSNAMELEN    5

MYDLL_API HWND mainHwnd = 0;

// This is an example of an exported function.
MYDLL_API void testFunction(void)
{
MessageBox(NULL, (LPCWSTR)L"Test", (LPCWSTR)L"Test", MB_OK);
}

MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
    if(wParam != NULL && (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP))
    {
        std::wstring s;
        MessageBox(NULL, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        GetClassName(m->hwnd, (LPWSTR) s.c_str(), CLASSNAMELEN);
        if(s == L"Edit")
            SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam);
    }
}

// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

main program

MYDLL_API HWND mainHwnd;
...
case WM_CREATE:
    {   
        // now it will load DLL and set up hook procedure for mouse events

        // declares local variables
        HOOKPROC hkprcMouseProc;
        HINSTANCE hinstDLL; 
        HHOOK hhookMouseProc;
        // loads DLL
        if((hinstDLL = LoadLibrary(TEXT("C:\\Users\\Francesco\\Dropbox\\poli\\bi\\not\\pds\\sp\\wk5\\lsp5\\Debug\\mydll.dll"))) == NULL)
        {
            MessageBox(hWnd, (LPCWSTR)L"Error loading DLL", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR);
            break;
        }

        // saves main window handle for DLL functions
        mainHwnd = hWnd;
...

Compiling I get

error LNK2001: unresolved external symbol __imp__mainHwnd

while using dumpbin /exports mydll.dll I get that global variable name is mangled as:

mainHwnd = _mainHwnd

I went through a lot of pages on google with no results. Maybe there is a conceptual error..
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2024-12-06 07:08:47

当您使用 LoadLibrary 在运行时加载 DLL 时,您无法直接在代码中访问其符号。 (这是因为这些符号必须在加载代码模块时修复,此时 LoadLibrary 尚未被调用,因此没有什么可以解析它们。链接器可以检测到此编译时就会出现这种情况,因此它友好地拒绝创建一个无论如何都会立即失败的 .exe)。

对于动态加载的 DLL,您需要使用 GetProcAddress 在运行时检索变量的地址。 (不管它的名字如何,它都适用于导出变量或导出函数,并且您最好确保将结果视为正确的事情!)

但我怀疑您并不真的想在运行时加载 DLL 。为什么不直接将主应用程序与生成的导入库链接起来?

When you're loading the DLL at run-time using LoadLibrary, you cannot access its symbols directly in your code. (This is because these symbols have to be fixed up when your code module is loaded, at which time LoadLibrary has not been called yet, so there is nothing to resolve them against. The linker can detect that this will be the case at compile time, so it friendlily refuses to create an .exe that would just fail immediately anyway).

For dynamically loaded DLLs, you need to use GetProcAddress to retrieve the address of the variable at run time. (Despite its name, it will work for either exported variables or exported functions, and you'd better be sure to treat the result as the right thing!)

But I suspect you don't really want to be loading the DLL at run time. Why don't you just link the main application with the generated import library?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文