如何动态调用在DLL中使用__declspec(dllexport)定义的功能? [英] How to dynamically call a funciton defined using __declspec(dllexport) in a DLL?

查看:116
本文介绍了如何动态调用在DLL中使用__declspec(dllexport)定义的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有
我是VC ++中DLL编程的新手,并且有个困扰我很长时间的问题.
情况是这样的:我在该项目中创建了一个DLL,如test.dll ,其中只有一个cpp文件 ,即test.cpp.在该.cpp文件中,我仅定义了一个名为void show()的函数,并将__declspec(dllexport)放在"void"之后,使定义如下所示:

all
I''m new to DLL programming in VC++ and have a question that bothers me for a long time.
Here is the situation:I created a DLL, like test.dll, with only one cpp file, test.cpp, in that project. in that .cpp file, i have only defined one function named void show(), and I put __declspec(dllexport) behind "void", make the definition looks like this:

void __declspec(dllexport) show()
{
   cout << "I am a DLL, and I''m called by you now!!" << endl;
}


编译完该项目后,我得到了一个test.dll文件.
现在的问题是如何动态调用该函数?
我可以使用LoadLibrary获取该dll的句柄,但不能使用GetProcAddress()获取该函数的地址.
当然,我包括了所有需要的头文件.
我错过了什么?还是我做错了什么?
任何答案或建议,将不胜感激.
谢谢.


After compiling this project, I got a test.dll file.
The question now is how to call that function dynamically?
I can get the handle of that dll using LoadLibrary, but can''t get the address of that function using GetProcAddress().
And of course I have all the needed header files included.
Did I miss something? or I did something wrong?
Any answers or suggestions will be appreciated.
Thanks.

推荐答案

这仅仅是名称修改的问题.您导出的名称是 not "show".要查看实际导出的内容,最好使用一些二进制转储工具,例如DUMPBIN.EXE(请参阅GetProcAddress的整齐的名称(请参阅 http ://msdn.microsoft.com/zh-CN/library/ms683212(v = vs.85).aspx [:

This is merely a problem of name mangling. You exported name is not "show". To see what is actually exported, it''s good to use some binary dump tool, such as DUMPBIN.EXE (see http://msdn.microsoft.com/en-us/library/c1h23y6c(v=VS.100).aspx[^]). It is bundled with every version of Visual Studio; you can run it using "Visual Studio Command Prompt". It will show you the mangled name you should pass to GetProcAddress (see http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx[^]).

To avoid name mangling, you can use extern "C":

#ifdef __cplusplus
extern "C" {  // only need to export C interface if
              // used by C++ source code
    void __declspec(dllexport) show() { /*...*/ }
#endif



名称修饰
名称修饰是C ++功能,可帮助开发人员假装他们在类型安全的环境中工作,该环境会检查功能的签名.在现实生活中,由于它们全都基于基于目标文件名的严重过时的链接,因此这仅仅是一个肮脏的把戏.可执行模块之间的实型安全性是几十年前发明和使用的.在现代,它以.NET meta-data的形式存在.

有关名称修饰的说明,请参见
http://en.wikipedia.org/wiki/Name_mangling [



Name mangling
or name decoration is the C++ feature helping developers to pretend they are working in type-safe environment, where the signatures of functions are checked. In real life, as it is all based on the gravely obsolete object file name-based linkage, this is no more than a dirty trick. Real type safety between executable modules was invented and used decades ago. In modern time it exists, for example, in the form of .NET meta-data.

For explanation of name mangling, see http://en.wikipedia.org/wiki/Name_mangling[^].

—SA


这篇关于如何动态调用在DLL中使用__declspec(dllexport)定义的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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