关于 GetProcAddress
我已经得到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用
extern "C"
来防止名称修改并确保函数已导出:要查看 DLL 的导出,您可以使用附带的
dumpbin.exe
实用程序使用 Visual Studio:这将列出所有导出符号的名称。
除此之外,不要指定以下任何一个编译器开关:
注意:我认为最后一个符号取决于编译器版本,但仍然不仅仅是“Myfunction”。
You need to use
extern "C"
to prevent name mangling and ensure the function is exported:To view the exports from your DLL you can use
dumpbin.exe
utility that is shipped with Visual Studio:This will list the names of all exported symbols.
In addition to this do not have either of the following compiler switches specified:
Note: I think last symbol is dependent on compiler version but is still not just "Myfunction".
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 tostdcall
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.
为什么使用
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.该符号将被修饰,因此它永远不会被称为
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).