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