是否可以获取exe内部函数的函数地址? [英] Is it possible to get the function address of a function that is inside an exe?

查看:184
本文介绍了是否可以获取exe内部函数的函数地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个针对Windows的书面代码,现在正尝试移植到Linux,但不确定是否可行.
将创建"ICantFindFile.txt",这意味着该文件无法加载.我选错文件了吗?

我对Linux还是很陌生,所以如果我的问题太低级,请和我一起露面.

这是编译输出:

Hi,
I have a written code for Windows, and now am trying to port to Linux, but not sure if it is possible or not.
"ICantFindFile.txt" is created, which means, the file couldn''t be loaded. Am I picking the wrong file?

I am very new to Linux, so please bare with me if my question is too low-level.

This is the compilation output:

  Output:
    build/release-linux-ppc64/ioq3ded.ppc64
    build/release-linux-ppc64/ioquake3.ppc64
    build/release-linux-ppc64/baseq3/cgameppc64.so
    build/release-linux-ppc64/baseq3/qagameppc64.so
    build/release-linux-ppc64/baseq3/uippc64.so
    build/release-linux-ppc64/missionpack/cgameppc64.so
    build/release-linux-ppc64/missionpack/qagameppc64.so
    build/release-linux-ppc64/missionpack/uippc64.so

make[2]: Entering directory `/r/home7/XXX/ioquake3''
make[2]: `build/release-linux-ppc64/ioq3ded.ppc64'' is up to date.
make[2]: `build/release-linux-ppc64/ioquake3.ppc64'' is up to date.
LD build/release-linux-ppc64/baseq3/cgameppc64.so
LD build/release-linux-ppc64/baseq3/qagameppc64.so
LD build/release-linux-ppc64/baseq3/uippc64.so
LD build/release-linux-ppc64/missionpack/cgameppc64.so
LD build/release-linux-ppc64/missionpack/qagameppc64.so
LD build/release-linux-ppc64/missionpack/uippc64.so




这是Windows的代码:




This is the code for Windows:

//Called function
__declspec(dllexport) void UnLinkLinkroutingcache( void )
{
      //code
}

//Callback location
#include<windows.h>
typedef void (* fUnLinkLinkroutingcache_t)( void );
void fUnLinkLinkroutingcache( fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache )
{
	pUnLinkLinkroutingcache(); return;
}
fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache;
void callUnlinkLink(void)
{
	HMODULE hLib;
	//fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache;

	hLib = LoadLibrary(TEXT("ioquake3.exe"));
	if (hLib == NULL)
		{
		//Module not found, permission denied, ...
		return 0; //inform caller of error
		}

	pUnLinkLinkroutingcache = (fUnLinkLinkroutingcache_t)GetProcAddress(hLib, TEXT("UnLinkLinkroutingcache"));
	if ( pUnLinkLinkroutingcache == NULL)
	{
		return 0;
	}

	fUnLinkLinkroutingcache(pUnLinkLinkroutingcache);
}



我试图将此代码移植到Linux,但似乎无法加载exe.



I tried to port this code to Linux, but I can''t seem to load the exe.

//Called function
extern void UnLinkLinkroutingcache( void )

//Callback location
void callUnlinkLink(void)
{
	void* handle;
	void (*initializer)(void);
	FILE *fp;//zgzg2020

	fp = fopen("HereIsMe.txt", "a");
	if( fp != NULL )
	{
		fprintf(fp, "%d", 1 );
		fprintf(fp, "\n" );
	}
	fclose(fp);

	handle = dlopen("./ioq3ded.ppc64", RTLD_LAZY);
	if(handle == NULL) {
		// report error ...
		fp = fopen("ICantFindFile.txt", "a");
		if( fp != NULL )
		{
			fprintf(fp, "%d", 1 );
			fprintf(fp, "\n" );
		}
		fclose(fp);
		exit(1);return;
	} else {
		initializer = dlsym(handle,"UnLinkLinkroutingcache");
		if(initializer == NULL) {
			// report error ...
			fp = fopen("ICantFindFfunction.txt", "a");
			if( fp != NULL )
			{
				fprintf(fp, "%d", 1 );
				fprintf(fp, "\n" );
			}
			fclose(fp);
			exit(1);return;
		} else {
			// cast initializer to its proper type and use
			(*initializer)();
		}
		// use the result in a call to dlsym
	}
}

推荐答案

您应该按照dlerror() >在这里 [ ^ ],当您dlsym()调用失败.当系统或api调用失败时,发布模糊的消息毫无意义.
You should be checking dlerror() as described here[^] when your dlopen() or dlsym() call fails. There is no point in posting vague messages when system or api calls fail.


我建​​议使用 dlerror [ ^ ]查看是否存在有关dlopen失败的原因的一些信息.
I suggest using dlerror[^] to see if there is some information about why dlopen failed.


这篇关于是否可以获取exe内部函数的函数地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆