Get the equivalent of GetModuleFileName on Linux

来源:互联网 发布:努比亚z9miniroot软件 编辑:程序博客网 时间:2024/05/22 11:37

GetModuleFileName is a windows API,

which is used to retrieves the fully qualified path for the file that contains the specified module.

The module must have been loaded by the current process.

 

On Linux SUSE, you can choose to use the following code block:

TCHAR tUserProfile[_MAX_PATH] = {0};char cUserProfile[_MAX_PATH] = {0};readlink("/proc/self/exe", cUserProfile,_MAX_PATH);long nBytesConverted = mbstowcs (tUserProfile, cUserProfile, _MAX_PATH);tUserProfile [nBytesConverted] = _T('\0'); 


 If your Module is a DLL, you can use the code below. It can also be applied to exe.

long_GetModuleFileName (TCHAR *exeName,long maxLen){char sLine[1024*2] = { 0 };void* pSymbol = (void*)"";long nBytesConverted = 0;FILE *fp = fopen ("/proc/self/maps", "r");if ( fp != NULL ){while( !feof (fp) ){if ( !fgets (sLine, sizeof (sLine), fp))continue;if ( !strstr (sLine, " r-xp ") || !strchr (sLine, '/'))continue;unsigned long start = 0, end = 0;sscanf (sLine, "%lx-%lx ", &start, &end);if (pSymbol >= (void *) start && pSymbol < (void *) end){char* pPath = strchr (sLine, '/');char* tmp = strrchr (pPath, '\n');if (tmp) *tmp = 0;nBytesConverted = mbstowcs (exeName, pPath, maxLen);  exeName [nBytesConverted] = _T('\0'); break;}}fclose (fp);}  return nBytesConverted;}


原创粉丝点击