DLL加载语法是否存在一些隐藏的问题? [英] DLL loading syntax Is there some hidden problem?

查看:68
本文介绍了DLL加载语法是否存在一些隐藏的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种语法来定义从DLL加载的函数类型





I use this syntax for defining type of functions loaded from DLL


typedef void* (*unknown)(
#ifdef __cplusplus
...
#endif
);





然后重新输入

unknown varname =(unknown) GetProcAddress(hGetProcIDDLL,FuncName);



这使我能够使用任何函数(int,int,int)(char *,int,char *)没有问题的例子。



我想知道是否存在一些不能使用的特殊情况,或者它不会像我解释的那样工作更高?



And then retype loads
unknown varname= (unknown)GetProcAddress(hGetProcIDDLL, "FuncName");

This gives me ability to use any function (int,int,int) (char*,int,char*) for example with no problems.

And I want to know if there is some special situation in which this can''t be used or it will not work the way I explained higher?

推荐答案

它应该适用于所有情况。为什么不?



但是,由于名称为hGetProcIDDLL,您的示例看起来很奇怪,因为第一个参数是获取的DLL模块ID,使用 LoadLibrary获得 LoadLibraryEx LoadPackagedLibrary GetModuleHandle

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx [ ^ ]。



因此,您的第一步应该是获取模块处理程序,因此您可以将其用作第一个参数。请参阅相应的MSDN帮助页面,从上面引用的页面开始。



-SA
It should work in all cases. Why not?

However, your sample looks strange due to the weird name "hGetProcIDDLL", because first parameter is the DLL module ID obtained, obtained using LoadLibrary, LoadLibraryEx, LoadPackagedLibrary, or GetModuleHandle:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx[^].

So, your first step should be obtaining the module handler, so you could use it as a first parameter. See the respective MSDN help pages, starting from the page referenced above.

—SA


虽然我从未尝试过,但我会说......这个小技巧很有可能会奏效。



但你是剥夺了编译器可以为您完成的所有类型检查。让我们说,你调用的函数有一个(int,int)的签名,而你用(char *,double,double)调用它。这可能会导致程序完全不可预测的行为。该功能甚至可能无法正常返回。这样的错误有时候很难调试。



所以,我建议:帮自己一个忙,并正确定义你的DLL中的函数类型,让编译器有机会检测参数序列中的错误。
Although I have never tried it, I''d say that there is a good chance that the little trick with "..." will work.

But you are depriving yourself from all the type checking that the compiler can do for you. Let''s say, the function you are calling has a signature of (int, int) and instead you call it with (char*, double, double). That might result in totally unpredictable behavior of the program. The function might not even return properly. And such errors are sometimes very hard to debug.

So, I''d recommend: Do yourself a favor and define the types of the functions in your DLL properly and give the compiler a chance to detect errors in the argument sequence.


这篇关于DLL加载语法是否存在一些隐藏的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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