From 59d54004f5aaf45221295b1a265c1f322c8718d0 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 23 Jan 2005 01:16:40 +0000 Subject: [PATCH] add GetProcessFileName --- .../archutils/Win32/GetFileInformation.cpp | 84 +++++++++++++++++++ .../src/archutils/Win32/GetFileInformation.h | 1 + 2 files changed, 85 insertions(+) diff --git a/stepmania/src/archutils/Win32/GetFileInformation.cpp b/stepmania/src/archutils/Win32/GetFileInformation.cpp index 40071a2eb4..8bfa2a5dea 100644 --- a/stepmania/src/archutils/Win32/GetFileInformation.cpp +++ b/stepmania/src/archutils/Win32/GetFileInformation.cpp @@ -4,6 +4,7 @@ #include "RageUtil.h" #include #include +#include #pragma comment(lib, "version.lib") @@ -78,6 +79,89 @@ CString FindSystemFile( CString fn ) return ""; } +/* Get the full path of the process running in iProcessID. On error, false is + * returned and an error message is placed in sName. */ +bool GetProcessFileName( uint32_t iProcessID, CString &sName ) +{ + /* This method works in everything except for NT4, and only uses kernel32.lib functions. */ + do { + HANDLE hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, iProcessID ); + if( hSnap == NULL ) + { + sName = werr_ssprintf( GetLastError(), "OpenProcess" ); + break; + } + + MODULEENTRY32 me; + ZERO( me ); + me.dwSize = sizeof(MODULEENTRY32); + bool bRet = !!Module32First( hSnap, &me ); + CloseHandle( hSnap ); + + if( bRet ) + { + sName = me.szExePath; + return true; + } + + sName = werr_ssprintf( GetLastError(), "Module32First" ); + } while(0); + + /* This method only works in NT/2K/XP. */ + do { + static HINSTANCE hPSApi = NULL; + typedef DWORD (WINAPI* pfnGetModuleFileNameEx)(HANDLE,HMODULE,LPSTR,DWORD); + static pfnGetModuleFileNameEx pGetModuleFileNameEx = NULL; + static bool bTried = false; + + if( !bTried ) + { + bTried = true; + + hPSApi = LoadLibrary("psapi.dll"); + if( hPSApi == NULL ) + { + sName = werr_ssprintf( GetLastError(), "LoadLibrary" ); + break; + } + else + { + pGetModuleFileNameEx = (pfnGetModuleFileNameEx) GetProcAddress( hPSApi, "GetModuleFileNameExA" ); + if( pGetModuleFileNameEx == NULL ) + { + sName = werr_ssprintf( GetLastError(), "GetProcAddress" ); + break; + } + } + } + + if( pGetModuleFileNameEx != NULL ) + { + HANDLE hProc = OpenProcess( PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, NULL, iProcessID ); + if( hProc == NULL ) + { + sName = werr_ssprintf( GetLastError(), "OpenProcess" ); + break; + } + + char buf[1024]; + int iRet = pGetModuleFileNameEx( hProc, NULL, buf, 1024 ); + CloseHandle( hProc ); + + if( iRet ) + { + buf[iRet] = 0; + sName = buf; + return true; + } + + sName = werr_ssprintf( GetLastError(), "GetModuleFileNameEx" ); + } + } while(0); + + return false; +} + /* * (c) 2004 Glenn Maynard * All rights reserved. diff --git a/stepmania/src/archutils/Win32/GetFileInformation.h b/stepmania/src/archutils/Win32/GetFileInformation.h index 7f73de36dd..84104810d2 100644 --- a/stepmania/src/archutils/Win32/GetFileInformation.h +++ b/stepmania/src/archutils/Win32/GetFileInformation.h @@ -5,6 +5,7 @@ bool GetFileVersion( CString fn, CString &out ); CString FindSystemFile( CString fn ); +bool GetProcessFileName( uint32_t iProcessID, CString &sName ); #endif