关于 GetProcAddress

发布于 2024-12-28 15:35:50 字数 847 浏览 0 评论 0原文

我已经得到了 MyDll.dll 及其定义如下的函数

void pascal Myfunction(BOOL);

,当我尝试在另一个项目中使用该函数时,我无法使用 GetProcAddress() 获取该函数的地址。这是我的代码:

void callMyDll()
{
 HINSTANCE hDll;

 hDll=LoadLibrary(_T("MyDll.dll");

 if(hDll!=NULL)
 {
  cout<<"\n DLL Loaded \n";
 }
 else
  cout<<"\n DLL Not loaded\n"

 typedef void (__stdcall *MyFunction)(bool)

 Myfunction mf1 = (MyFunction) GetProcAddress(hDll, "MyFunction");

 if (mf1!=NULL)
  cout<<"\n Function Loaded Successfully \n";
 else
  cout<<"\n Function not loaded \n";

 FreeLibrary(hDll);
}

我得到的输出为:

DLL Loaded
Function not loaded

但是当我尝试使用已知的 DLL(如 glut32.dll 及其函数)时,它工作正常。

我认为它的功能可能有问题,例如

void pascal MyFunction(BOOL);

有人可以在这方面帮助我吗?

I have got MyDll.dll and its function defined as below

void pascal Myfunction(BOOL);

when I'm trying to use the function in another project i am unable get the address of the function with GetProcAddress(). Here is my code:

void callMyDll()
{
 HINSTANCE hDll;

 hDll=LoadLibrary(_T("MyDll.dll");

 if(hDll!=NULL)
 {
  cout<<"\n DLL Loaded \n";
 }
 else
  cout<<"\n DLL Not loaded\n"

 typedef void (__stdcall *MyFunction)(bool)

 Myfunction mf1 = (MyFunction) GetProcAddress(hDll, "MyFunction");

 if (mf1!=NULL)
  cout<<"\n Function Loaded Successfully \n";
 else
  cout<<"\n Function not loaded \n";

 FreeLibrary(hDll);
}

I'm getting output as:

DLL Loaded
Function not loaded

But when I'm trying with known DLLs like glut32.dll and its functions it is working fine.

I think it may be problem with its function like

void pascal MyFunction(BOOL);

Can anybody help me in this regard?

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

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

发布评论

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

评论(4

伴我老 2025-01-04 15:35:50

您需要使用 extern "C" 来防止名称修改并确保函数已导出:

extern "C" __declspec(dllexport) void Myfunction(BOOL);

要查看 DLL 的导出,您可以使用附带的 dumpbin.exe 实用程序使用 Visual Studio:

dumpbin.exe /EXPORTS MyDll.dll

这将列出所有导出符号的名称。

除此之外,不要指定以下任何一个编译器开关:

Gz __stdcall calling convention: "Myfunction" would be exported as Myfunction@4
Gr __fastcall caling convention: "Myfunction" would be exported as @Myfunction@4

注意:我认为最后一个符号取决于编译器版本,但仍然不仅仅是“Myfunction”。

You need to use extern "C" to prevent name mangling and ensure the function is exported:

extern "C" __declspec(dllexport) void Myfunction(BOOL);

To view the exports from your DLL you can use dumpbin.exe utility that is shipped with Visual Studio:

dumpbin.exe /EXPORTS MyDll.dll

This will list the names of all exported symbols.

In addition to this do not have either of the following compiler switches specified:

Gz __stdcall calling convention: "Myfunction" would be exported as Myfunction@4
Gr __fastcall caling convention: "Myfunction" would be exported as @Myfunction@4

Note: I think last symbol is dependent on compiler version but is still not just "Myfunction".

北方。的韩爷 2025-01-04 15:35:50

DLL 导出过程会受到名称修改和修饰的影响。早已过时的 16 位 pascal 调用约定相当于 32 位平台上的 stdcall

首先,您应该使用 extern "C" 来指定 C 链接并禁用名称修改。

但是,您的函数仍将受到名称修饰的约束。如果您使用 __declspec(dllexport) 导出它,那么它实际上会以名称 _Myfunction@4 导出。如果您希望按其真实名称导出它,则需要使用 .def 文件。

但是,您仍然有可能根本没有从 DLL 中导出该函数。使用 Dependency Walker 检查它是否已导出,如果是,则以什么名称导出。

The DLL export process is subject to name mangling and decoration. The long obsolete 16 bit pascal calling convention is equivalent to stdcall on 32 bit platforms.

First of all you should use extern "C" to specify C linkage and disable name mangling.

However, your function will still be subject to name decoration. If you export it with __declspec(dllexport) then it will in fact be exported with the name _Myfunction@4. If you wish to export it by its true name then you need to use a .def file.

However, the possibility still remains that you did not export the function from the DLL at all. Use Dependency Walker to check whether it was exported, and if so by what name.

冬天旳寂寞 2025-01-04 15:35:50

为什么使用 pascal 调用约定?也许这会改变符号的名称,如果是这样,您可能需要考虑到这一点。

Why are you using the pascal calling-convention? Perhaps that alters the names of symbols, and if so you might need to take that into account.

温柔戏命师 2025-01-04 15:35:50

该符号将被修饰,因此它永远不会被称为 MyFunction,它更可能是 _MyFunction@4。您可以使用 dumpbin 之类的工具快速检查这一点。

您可以在此处阅读有关损坏的更多信息,如果您想避免损坏,则需要使用def 文件来指定符号名称(或序数)。

The symbol is going to be decorated, so it will never be called MyFunction, its more likely _MyFunction@4. you can quickly check this using something like dumpbin.

You can read up more on mangling here, if you want to avoid mangling, you need to use a def file to specify symbol names (or ordinals).

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