1 #include <QCoreApplication>
2 #include <QDir>
3
4 #ifndef _UNICODE
5 #define _UNICODE
6 #endif
7
8 #include "x64.hh"
9 #include <windows.h>
10 #include <tchar.h>
11
12 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
13 PROCESS_INFORMATION pInfo;
14
15 #ifndef Q_OS_WIN64
isWow64()16 bool isWow64()
17 {
18 static LPFN_ISWOW64PROCESS fnIsWow64Process;
19 BOOL bIsWow64 = FALSE;
20
21 if( NULL == fnIsWow64Process )
22 fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle( _T("kernel32") ), "IsWow64Process" );
23 if( NULL != fnIsWow64Process )
24 {
25 if ( !fnIsWow64Process( GetCurrentProcess(), &bIsWow64 ) )
26 return false;
27 }
28 return bIsWow64;
29 }
30 #endif
31
installx64Hooks()32 bool installx64Hooks()
33 {
34 STARTUPINFO startup;
35 #ifndef Q_OS_WIN64
36 if( !isWow64() )
37 return false;
38 #endif
39 if( pInfo.hProcess != NULL )
40 removex64Hooks();
41 QDir dir = QCoreApplication::applicationDirPath();
42 #ifdef Q_OS_WIN64
43 if( !dir.cd("x86") )
44 return false;
45 QString starterProc = QDir::toNativeSeparators( dir.filePath( "x86helper.exe" ) );
46 #else
47 if( !dir.cd("x64") )
48 return false;
49 QString starterProc = QDir::toNativeSeparators( dir.filePath( "x64helper.exe" ) );
50 #endif
51
52 memset( &startup, 0, sizeof(startup) );
53 startup.cb = sizeof(startup);
54
55 BOOL b = CreateProcess( starterProc.toStdWString().c_str(), NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW | DETACHED_PROCESS, NULL, NULL, &startup, &pInfo );
56 if( !b )
57 pInfo.hProcess = NULL;
58
59 return b;
60 }
61
removex64Hooks()62 void removex64Hooks()
63 {
64 if( pInfo.hProcess == NULL )
65 return;
66 PostThreadMessage( pInfo.dwThreadId, WM_QUIT, 0, 0 );
67 DWORD res = WaitForSingleObject( pInfo.hProcess, 3000 );
68 if( res == WAIT_TIMEOUT )
69 TerminateProcess( pInfo.hProcess, 1 );
70 CloseHandle( pInfo.hProcess );
71 CloseHandle( pInfo.hThread );
72 pInfo.hProcess = NULL;
73 }
74