本文介绍了GetProcAddress函数在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好:我加载了我的DLL在我的项目,但每当我使用GetProcAddress函数。它返回NULL!我该怎么办? 我在MYDLL.dll中使用此函数(double GetNumber(double x)) 这里是我使用的代码: typedef double(* LPGETNUMBER)(double Nbr); HINSTANCE hDLL = NULL; LPGETNUMBER lpGetNumber; hDLL = LoadLibrary(LMYDLL.DLL); lpGetNumber =(LPGETNUMBER)GetProcAddress((HMODULE)hDLL,GetNumber); 解决方案检查返回码并调用 GetLastError()将让你免费。你应该在这里检查两次返回码。您实际上检查返回代码为零次。 hDLL = LoadLibrary(LMYDLL.DLL); 检查 hDLL 。是NULL吗?如果是这样,调用 GetLastError()找出原因。 lpGetNumber =(LPGETNUMBER)GetProcAddress((HMODULE)hDLL,GetNumber ); 如果 lpGetNumber 为NULL, c $ c> GetLastError()。它会告诉你为什么找不到proc地址。有几种可能的情况: 没有 / code> 有一个名为 GetNumber 的导出函数,但没有标记 extern c,导致名称损坏。 hDLL 不是有效的库句柄。 要成为#1上面,你需要导出函数通过装饰与 __ declspec(dllexport)的声明像这样: MyFile.h __ declspec(dllexport)int GetNumber(); 如果原来是#2,你需要这样做: externC { __declspec(dllexport)int 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"); 解决方案 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.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".lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");If lpGetNumber is NULL, call GetLastError(). It will tell you why the proc address could not be found. There are a few likely scenarios:There is no exported function named GetNumberThere is an exported function named GetNumber, but it is not marked extern "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__declspec(dllexport) int GetNumber();If it turns out to be #2 above, you need to do this:extern "C"{ __declspec(dllexport) int GetNumber();}; 这篇关于GetProcAddress函数在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 10:27