C++ 中的 GetProcAddress 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6031431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetProcAddress function in C++
提问by Alireza
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"
大家好:我已经在我的项目中加载了我的 DLL,但是每当我使用 GetProcAddress 函数时。它返回NULL!我该怎么办?我在“MYDLL.dll”中使用这个函数( double GetNumber(double x) )
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");
回答by John Dibling
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.
检查返回代码和呼叫GetLastError()将使您自由。您应该在这里检查两次返回码。您实际上是在检查返回代码零次。
hDLL = LoadLibrary(L"MYDLL.DLL");
Check hDLL. Is it NULL? If so, call GetLastError()to find out why. It may be as simple as "File Not Found".
检查hDLL。它是NULL吗?如果是这样,请致电GetLastError()了解原因。它可能像“找不到文件”一样简单。
lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");
If lpGetNumberis NULL, call GetLastError(). It will tell you why the proc address could not be found. There are a few likely scenarios:
如果lpGetNumber为 NULL,则调用GetLastError()。它会告诉你为什么找不到 proc 地址。有几种可能的情况:
- There is no exportedfunction named
GetNumber - There is an exported function named
GetNumber, but it is not markedextern "c", resulting in name mangling. hDLLisn't a valid library handle.
- 没有名为的导出函数
GetNumber - 有一个名为 的导出函数
GetNumber,但它没有被标记extern "c",导致名称修改。 hDLL不是有效的库句柄。
If it turns out to be #1 above, you need to export the functions by decorating the declaration with __declspec(dllexport)like this:
如果结果是上面的#1,则需要通过如下装饰声明来导出函数__declspec(dllexport):
MyFile.h
我的文件
__declspec(dllexport) int GetNumber();
If it turns out to be #2 above, you need to do this:
如果结果是上面的#2,你需要这样做:
extern "C"
{
__declspec(dllexport) int GetNumber();
};
回答by karlphillip
Most probably LoadLibrary()failed. You just can't see that because apparently you are not checking what it is returning:
很可能LoadLibrary()失败了。您只是看不到这一点,因为显然您没有检查它返回的内容:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
如果函数失败,则返回值为 NULL。要获取扩展错误信息,请调用 GetLastError。
EDIT:
编辑:
We don't know how you are exporting the function on the DLL code, but this threadexplains a couple of reasons on why GetProcAddress fails.
我们不知道您是如何在 DLL 代码上导出函数的,但是该线程解释了 GetProcAddress 失败的几个原因。
回答by user1667687
You might want to check if your GetNumberfunction is exported as an __stdcallfunction.
您可能想要检查您的GetNumber函数是否作为__stdcall函数导出。
If so, try GetProcAddress(hDLL, "_GetNumber@N");, where Nis the total number of bytes of GetNumber's argument list. For example, if your function signature is int GetNumber(int a, double b), its real name in DLL will be _GetNumber@12.
如果是,请 try GetProcAddress(hDLL, "_GetNumber@N");,其中N是GetNumber的参数列表的总字节数。例如,如果您的函数签名是int GetNumber(int a, double b),则其在 DLL 中的真实名称将是_GetNumber@12。
Reference: __stdcall
参考:__stdcall

