1*fc0bc008SAndrew Rist /**************************************************************
2*fc0bc008SAndrew Rist  *
3*fc0bc008SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*fc0bc008SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*fc0bc008SAndrew Rist  * distributed with this work for additional information
6*fc0bc008SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*fc0bc008SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*fc0bc008SAndrew Rist  * "License"); you may not use this file except in compliance
9*fc0bc008SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*fc0bc008SAndrew Rist  *
11*fc0bc008SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*fc0bc008SAndrew Rist  *
13*fc0bc008SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*fc0bc008SAndrew Rist  * software distributed under the License is distributed on an
15*fc0bc008SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*fc0bc008SAndrew Rist  * KIND, either express or implied.  See the License for the
17*fc0bc008SAndrew Rist  * specific language governing permissions and limitations
18*fc0bc008SAndrew Rist  * under the License.
19*fc0bc008SAndrew Rist  *
20*fc0bc008SAndrew Rist  *************************************************************/
21*fc0bc008SAndrew Rist 
22cdf0e10cSrcweir #include "macros.h"
23cdf0e10cSrcweir #ifdef _MSC_VER
24cdf0e10cSrcweir #pragma warning(push,1) // disable warnings within system headers
25cdf0e10cSrcweir #endif
26cdf0e10cSrcweir #include <psapi.h>
27cdf0e10cSrcweir #ifdef _MSC_VER
28cdf0e10cSrcweir #pragma warning(pop)
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <tlhelp32.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir IMPLEMENT_THUNK( psapi, WINDOWS, DWORD, WINAPI, GetModuleFileNameExA, (HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize ) )
33cdf0e10cSrcweir {
34cdf0e10cSrcweir 	DWORD	dwProcessId = 0;
35cdf0e10cSrcweir 	DWORD	dwResult = 0;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	if ( !hProcess || hProcess == GetCurrentProcess() || GetCurrentProcessId() == (dwProcessId = GetProcessId( hProcess )) )
38cdf0e10cSrcweir 		return GetModuleFileNameA( hModule, lpFileName, nSize );
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 	HANDLE	hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 	if ( IsValidHandle( hSnapshot ) )
43cdf0e10cSrcweir 	{
44cdf0e10cSrcweir 		MODULEENTRY32	me;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 		me.dwSize = sizeof(me);
47cdf0e10cSrcweir 		if ( Module32First( hSnapshot, &me ) )
48cdf0e10cSrcweir 		{
49cdf0e10cSrcweir 			BOOL	fFound = FALSE;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 			if ( NULL == hModule )
52cdf0e10cSrcweir 				fFound = TRUE;
53cdf0e10cSrcweir 			else do
54cdf0e10cSrcweir 			{
55cdf0e10cSrcweir 				fFound = (me.hModule == hModule);
56cdf0e10cSrcweir 			} while ( !fFound && Module32Next( hSnapshot, &me ) );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 			if ( fFound )
59cdf0e10cSrcweir 			{
60cdf0e10cSrcweir 				dwResult = _tcslen( me.szExePath );
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 				if ( dwResult > nSize && nSize > 0 )
63cdf0e10cSrcweir 					lpFileName[nSize -1] = 0;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 				_tcsncpy( lpFileName, me.szExePath, nSize );
66cdf0e10cSrcweir 			}
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 		CloseHandle( hSnapshot );
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	return dwResult;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75