查找名称错位功能的动态加载的DLL [英] Find name-mangled function in dynamically-loaded dll

查看:110
本文介绍了查找名称错位功能的动态加载的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组明确设计为用C仅用于++的API。我不希望他们通过一个C程序(或任何其他语言的这个问题),并且相对于打算在的externC路线和使用内联效用函数调用简单的C函数。

I have a set of APIs that are explicitly designed to be used only in C++. I do not expect them to be used by a C program (or any other language for that matter), and as such I export namespace and class information as opposed to going the extern "C" route and using inlined utility functions to call the plain C functions.

现在我唯一的工作上是在编译时链接DLL文件,这意味着导入功能,可执行文件是很容易的,因为它涉及我的部分没有工作。不过,我计划开发一个插件系统,这将需要我在运行时动态加载的DLL。我将能够使用,以查找姓名,错位C ++函数 GetProcAddress的()

Right now I am only working on dlls that are linked at compile time, which means importing the functions to the executable is very easy as it involves no work on my part. However, I plan on developing a plugin system which will require me to load dlls dynamically at run time. Will I be able to find name-mangled C++ functions using GetProcAddress()?

推荐答案

你在做什么是<一个href=\"http://stackoverflow.com/questions/22797418/how-do-i-safely-pass-objects-especially-stl-objects-to-and-from-a-dll\">not不一定是个好主意除非你能掌控整个构建链,可以保证双方的DLL和使用它的任何应用程序都建有相同的版本相同的编译器。

What you're doing is not necessarily a good idea unless you control the entire build chain and can ensure both your DLL and any apps using it are built with the same version of the same compiler.

随着中说,是的,你可以使用加载名称错位功能 GetProcAddress的。只需使用的Dependency Walker 或看看你的DLL生成的DEF文件,如果你的编译器设置为生产一体,得到错位函数名。然后你可以 GetProcAddress的它。你与重整的名称不能,然而,通话 GetProcAddress的并期望它找到合适的重整名称。例如,如果你的DLL函数被命名为添加,是错位的,以 _Z3Addv ,你就需要调用 GetProcAddress的(MYDLL_Z3Addv); 来正确地访问功能

With that said, yes, you can load name-mangled functions using GetProcAddress. Just use Dependency Walker or look at the generated .def file for your DLL, if your compiler is set to produce one, to get the mangled function name. Then you can GetProcAddress it. You cannot, however, call GetProcAddress with an unmangled name and expect it to find the right mangled name. For example, if your DLL's function is named Add, and is mangled to _Z3Addv, you would need to call GetProcAddress(myDLL, "_Z3Addv"); to access the function properly.

您将需要更改调用 GetProcAddress的每次你改变你的函数的声明时,由于重整名称也将改变。要知道,你还需要,如果你改变你的DLL是建立与编译器修改 GetProcAddress的通话 - MSVC的重整是从GCC的一个很大的不同,以及铿锵的重整是可能不同从他们两个。所以,你可能要重新考虑你这样做的方式,因为它似乎相当容易沿途某处破

You would need to change the call to GetProcAddress every time you change the declaration of your function, since the mangled name would also change. Be aware you will also need to change the GetProcAddress call if you change the compiler your DLL is built with - MSVC's mangling is a lot different from GCC's, and clang's mangling is probably different from both of them. So you might want to reconsider the way you're doing this, since it seems rather prone to breaking somewhere along the way.

这篇关于查找名称错位功能的动态加载的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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