2 #include "xkeymacs64.h"
\r
3 #include "mainfrm64.h"
\r
5 CXkeymacsApp::CXkeymacsApp()
\r
10 CXkeymacsApp theApp;
\r
12 static UINT PollIPCMessage(LPVOID lpParam);
\r
13 static void Start32bitProcess();
\r
15 BOOL CXkeymacsApp::InitInstance()
\r
17 m_hMutex = CreateMutex(NULL, FALSE, _T("XKeymacs64"));
\r
18 if (GetLastError() == ERROR_ALREADY_EXISTS) {
\r
19 CloseHandle(m_hMutex);
\r
23 bool start32bit = false;
\r
24 HANDLE h32 = CreateMutex(NULL, FALSE, _T("XKeymacs"));
\r
25 if (GetLastError() != ERROR_ALREADY_EXISTS)
\r
29 m_pMainWnd = new CMainFrame;
\r
30 m_pMainWnd->ShowWindow(SW_HIDE);
\r
31 m_pMainWnd->UpdateWindow();
\r
33 CUtils::InitCUtils();
\r
34 AfxBeginThread(PollIPCMessage, NULL);
\r
36 Start32bitProcess();
\r
37 CXkeymacsDll::SetHooks();
\r
41 UINT PollIPCMessage(LPVOID lpParam)
\r
43 HANDLE hPipe = CreateNamedPipe(IPC_PIPE, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 512, 512, 0, NULL);
\r
44 if (hPipe == INVALID_HANDLE_VALUE)
\r
47 if (ConnectNamedPipe(hPipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED))
\r
51 if (!ReadFile(hPipe, &msg, sizeof(msg), &read, NULL) || read != sizeof(msg))
\r
53 DWORD written, nul = 0;
\r
54 if (!WriteFile(hPipe, &nul, sizeof(DWORD), &written, NULL) || written != sizeof(DWORD)
\r
55 || !FlushFileBuffers(hPipe) || !DisconnectNamedPipe(hPipe))
\r
62 case XKEYMACS_RELOAD:
\r
63 CXkeymacsDll::LoadConfig();
\r
65 case XKEYMACS_RESET:
\r
66 CXkeymacsDll::ResetHooks();
\r
72 CXkeymacsDll::ReleaseHooks();
\r
77 void Start32bitProcess()
\r
79 TCHAR buf[MAX_PATH];
\r
80 if (!GetModuleFileName(NULL, buf, MAX_PATH))
\r
83 if (!path.Replace(_T("xkeymacs64.exe"), _T("xkeymacs.exe")))
\r
86 ZeroMemory(&si, sizeof(si));
\r
88 PROCESS_INFORMATION pi;
\r
89 ZeroMemory(&pi, sizeof(pi));
\r
90 if (!CreateProcess(path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
\r
92 // close unused handles
\r
93 CloseHandle(pi.hProcess);
\r
94 CloseHandle(pi.hThread);
\r
97 int CXkeymacsApp::ExitInstance()
\r
100 CloseHandle(m_hMutex);
\r
102 m_pMainWnd->DestroyWindow();
\r
107 return CWinApp::ExitInstance();
\r