本文主要是介绍GetProcAddress()失败 GetLastError 127,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
有错误127的代码如下:
#include<windows.h>#include<stdio.h>//#pragma comment(linker,"/export:add=dllA_old.add")HMODULE hModule=NULL; //模块句柄FARPROC proc; //函数入口指针typedef int (__cdecl *Add)(int a,int b);Add padd;BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ if(NULL==hModule) { hModule=LoadLibrary("dllA_old.dll"); } if(NULL==hModule) { MessageBoxA(NULL,"获取dllA_old.dll失败","caption",0); return 0; } return TRUE;}int add(int a,int b){ padd=(Add)GetProcAddress(hModule,"add"); //这里获取函数地址失败 127 if(NULL==padd) { char msg[256]; sprintf(msg,"获取add()失败:%d",GetLastError()); MessageBoxA(NULL,msg,"caption",0); return 0; } return padd(a,b);}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
修改方法,就是在add函数中,进行加装dll操作,而不是在dll的入口处加载dll文件,代码如下:
#include<windows.h>#include<stdio.h>//#pragma comment(linker,"/export:add=dllA_old.add")HMODULE hModule=NULL; //模块句柄FARPROC proc; //函数入口指针typedef int (__cdecl *Add)(int a,int b);Add padd;BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ if(NULL==hModule) { hModule=LoadLibrary("dllA_old.dll"); } if(NULL==hModule) { MessageBoxA(NULL,"获取dllA_old.dll失败","caption",0); return 0; } return TRUE;}int add(int a,int b){ hModule=LoadLibrary("dllA_old.dll"); //在这里加载哈 padd=(Add)GetProcAddress(hModule,"add"); if(NULL==padd) { char msg[256]; sprintf(msg,"获取add()失败:%d",GetLastError()); MessageBoxA(NULL,msg,"caption",0); return 0; } return padd(a,b);}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
您的十分满意是我追求的宗旨。
您的一点建议是我后续的动力。
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这篇关于GetProcAddress()失败 GetLastError 127的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!