merge the formfield patch from ooo-build
[ooovba.git] / sal / systools / win32 / uwinapi / GetModuleFileNameExA.cpp
blob8b5d5b5d57a39ca74736ef21edde86413d33b5e4
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>
11 IMPLEMENT_THUNK( psapi, WINDOWS, DWORD, WINAPI, GetModuleFileNameExA, (HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize ) )
13 DWORD dwProcessId = 0;
14 DWORD dwResult = 0;
16 if ( !hProcess || hProcess == GetCurrentProcess() || GetCurrentProcessId() == (dwProcessId = GetProcessId( hProcess )) )
17 return GetModuleFileNameA( hModule, lpFileName, nSize );
19 HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
21 if ( IsValidHandle( hSnapshot ) )
23 MODULEENTRY32 me;
25 me.dwSize = sizeof(me);
26 if ( Module32First( hSnapshot, &me ) )
28 BOOL fFound = FALSE;
30 if ( NULL == hModule )
31 fFound = TRUE;
32 else do
34 fFound = (me.hModule == hModule);
35 } while ( !fFound && Module32Next( hSnapshot, &me ) );
37 if ( fFound )
39 dwResult = _tcslen( me.szExePath );
41 if ( dwResult > nSize && nSize > 0 )
42 lpFileName[nSize -1] = 0;
44 _tcsncpy( lpFileName, me.szExePath, nSize );
48 CloseHandle( hSnapshot );
51 return dwResult;