C++ 中的 GetProcAddress 函数
大家好:我已经在项目中加载了 DLL,但是每当我使用 GetProcAddress 函数时。它返回 NULL!我应该怎么办? 我在“MYDLL.dll”中使用这个函数( double GetNumber(double x) )
这是我使用的代码:
typedef double (*LPGETNUMBER)(double Nbr);
HINSTANCE hDLL = NULL;
LPGETNUMBER lpGetNumber;
hDLL = LoadLibrary(L"MYDLL.DLL");
lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");
Hello guys: I've loaded my DLL in my project but whenever I use the GetProcAddress function. it returns NULL! what should I do?
I use this function ( double GetNumber(double x) ) in "MYDLL.dll"
Here is a code which I used:
typedef double (*LPGETNUMBER)(double Nbr);
HINSTANCE hDLL = NULL;
LPGETNUMBER lpGetNumber;
hDLL = LoadLibrary(L"MYDLL.DLL");
lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查返回代码并调用
GetLastError()
将使您自由。您应该在此处检查返回代码两次。您实际上检查返回码零次。检查
hDLL
。是NULL吗?如果是这样,请调用GetLastError()
找出原因。它可能像“找不到文件”一样简单。如果
lpGetNumber
为NULL,则调用GetLastError()
。它会告诉你为什么找不到 proc 地址。有几种可能的情况:GetNumber
的导出函数GetNumber
的导出函数,但未标记为extern "c"
,导致名称修改。hDLL
不是有效的库句柄。如果结果是上面的#1,则需要通过用
__declspec(dllexport)
修饰声明来导出函数,如下所示:MyFile.h
如果结果是上面的#2,则需要为此:
Checking return codes and calling
GetLastError()
will set you free. You should be checking return codes twice here. You are actually checking return codes zero times.Check
hDLL
. Is it NULL? If so, callGetLastError()
to find out why. It may be as simple as "File Not Found".If
lpGetNumber
is NULL, callGetLastError()
. It will tell you why the proc address could not be found. There are a few likely scenarios:GetNumber
GetNumber
, but it is not markedextern "c"
, resulting in name mangling.hDLL
isn't a valid library handle.If it turns out to be #1 above, you need to export the functions by decorating the declaration with
__declspec(dllexport)
like this:MyFile.h
If it turns out to be #2 above, you need to do this:
您可能想检查您的
GetNumber
函数是否导出为__stdcall
函数。如果是这样,请尝试
GetProcAddress(hDLL, "_GetNumber@N");
,其中N
是GetNumber
参数的总字节数列表。例如,如果您的函数签名是int GetNumber(int a, double b)
,则它在DLL中的真实名称将为_GetNumber@12
。参考:__stdcall
You might want to check if your
GetNumber
function is exported as an__stdcall
function.If so, try
GetProcAddress(hDLL, "_GetNumber@N");
, whereN
is the total number of bytes ofGetNumber
's argument list. For example, if your function signature isint GetNumber(int a, double b)
, its real name in DLL will be_GetNumber@12
.Reference: __stdcall
最有可能的是
LoadLibrary()
失败。你只是看不到这一点,因为显然你没有检查它返回的内容:编辑:
我们不知道您如何导出 DLL 代码上的函数,但是 此线程 解释了 GetProcAddress 失败的几个原因。
Most probably
LoadLibrary()
failed. You just can't see that because apparently you are not checking what it is returning:EDIT:
We don't know how you are exporting the function on the DLL code, but this thread explains a couple of reasons on why GetProcAddress fails.