GetProcAddress()失败 GetLastError() 127

有错误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);
}


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

您的十分满意是我追求的宗旨。

您的一点建议是我后续的动力。

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(dll,127,GetLastError,loadlibrary,GetProcAddress)