1 #include "macros.h"
2 #ifdef _MSC_VER
3 #pragma warning(push,1) // disable warnings within system headers
4 #endif
5 #include <psapi.h>
6 #ifdef _MSC_VER
7 #pragma warning(pop)
8 #endif
9 #include <tlhelp32.h>
10 
11 IMPLEMENT_THUNK( psapi, WINDOWS, DWORD, WINAPI, GetModuleFileNameExA, (HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize ) )
12 {
13 	DWORD	dwProcessId = 0;
14 	DWORD	dwResult = 0;
15 
16 	if ( !hProcess || hProcess == GetCurrentProcess() || GetCurrentProcessId() == (dwProcessId = GetProcessId( hProcess )) )
17 		return GetModuleFileNameA( hModule, lpFileName, nSize );
18 
19 	HANDLE	hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
20 
21 	if ( IsValidHandle( hSnapshot ) )
22 	{
23 		MODULEENTRY32	me;
24 
25 		me.dwSize = sizeof(me);
26 		if ( Module32First( hSnapshot, &me ) )
27 		{
28 			BOOL	fFound = FALSE;
29 
30 			if ( NULL == hModule )
31 				fFound = TRUE;
32 			else do
33 			{
34 				fFound = (me.hModule == hModule);
35 			} while ( !fFound && Module32Next( hSnapshot, &me ) );
36 
37 			if ( fFound )
38 			{
39 				dwResult = _tcslen( me.szExePath );
40 
41 				if ( dwResult > nSize && nSize > 0 )
42 					lpFileName[nSize -1] = 0;
43 
44 				_tcsncpy( lpFileName, me.szExePath, nSize );
45 			}
46 		}
47 
48 		CloseHandle( hSnapshot );
49 	}
50 
51 	return dwResult;
52 }
53 
54