0962713: use GetProcessImageFileName instead of GetModuleFileNameEx; it works in more cases, eg. when losing focus to the taskbar [Glenn Maynard]

This commit is contained in:
AJ Kelly
2010-07-11 13:53:03 -05:00
parent 287712678e
commit 9f298ec36a
+10 -9
View File
@@ -96,7 +96,7 @@ bool GetProcessFileName( uint32_t iProcessID, RString &sName )
HANDLE hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, iProcessID ); HANDLE hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, iProcessID );
if( hSnap == NULL ) if( hSnap == NULL )
{ {
sName = werr_ssprintf( GetLastError(), "OpenProcess" ); sName = werr_ssprintf( GetLastError(), "CreateToolhelp32Snapshot" );
break; break;
} }
@@ -118,8 +118,8 @@ bool GetProcessFileName( uint32_t iProcessID, RString &sName )
/* This method only works in NT/2K/XP. */ /* This method only works in NT/2K/XP. */
do { do {
static HINSTANCE hPSApi = NULL; static HINSTANCE hPSApi = NULL;
typedef DWORD (WINAPI* pfnGetModuleFileNameEx)(HANDLE,HMODULE,LPSTR,DWORD); typedef DWORD (WINAPI* pfnGetProcessImageFileNameA)(HANDLE hProcess, LPSTR lpImageFileName, DWORD nSize);
static pfnGetModuleFileNameEx pGetModuleFileNameEx = NULL; static pfnGetProcessImageFileNameA pGetProcessImageFileName = NULL;
static bool bTried = false; static bool bTried = false;
if( !bTried ) if( !bTried )
@@ -134,8 +134,8 @@ bool GetProcessFileName( uint32_t iProcessID, RString &sName )
} }
else else
{ {
pGetModuleFileNameEx = (pfnGetModuleFileNameEx) GetProcAddress( hPSApi, "GetModuleFileNameExA" ); pGetProcessImageFileName = (pfnGetProcessImageFileNameA) GetProcAddress( hPSApi, "GetProcessImageFileNameA" );
if( pGetModuleFileNameEx == NULL ) if( pGetProcessImageFileName == NULL )
{ {
sName = werr_ssprintf( GetLastError(), "GetProcAddress" ); sName = werr_ssprintf( GetLastError(), "GetProcAddress" );
break; break;
@@ -143,7 +143,7 @@ bool GetProcessFileName( uint32_t iProcessID, RString &sName )
} }
} }
if( pGetModuleFileNameEx != NULL ) if( pGetProcessImageFileName != NULL )
{ {
HANDLE hProc = OpenProcess( PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, NULL, iProcessID ); HANDLE hProc = OpenProcess( PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, NULL, iProcessID );
if( hProc == NULL ) if( hProc == NULL )
@@ -153,17 +153,18 @@ bool GetProcessFileName( uint32_t iProcessID, RString &sName )
} }
char buf[1024]; char buf[1024];
int iRet = pGetModuleFileNameEx( hProc, NULL, buf, 1024 ); int iRet = pGetProcessImageFileName( hProc, buf, sizeof(buf) );
CloseHandle( hProc ); CloseHandle( hProc );
if( iRet ) if( iRet )
{ {
buf[iRet] = 0; if( iRet == sizeof(buf) )
buf[iRet-1] = 0;
sName = buf; sName = buf;
return true; return true;
} }
sName = werr_ssprintf( GetLastError(), "GetModuleFileNameEx" ); sName = werr_ssprintf( GetLastError(), "GetProcessImageFileName" );
} }
} while(0); } while(0);