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