OSDN Git Service

Make the timeout of CallNamedPipe with IPC_PIPE forever.
[xkeymacs/xkeymacs.git] / xkeymacs / xkeymacs.cpp
1 // xkeymacs.cpp : Defines the class behaviors for the application.\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "xkeymacs.h"\r
6 #include "Profile.h"\r
7 #include "MainFrm.h"\r
8 \r
9 #ifdef _DEBUG\r
10 #define new DEBUG_NEW\r
11 #undef THIS_FILE\r
12 static char THIS_FILE[] = __FILE__;\r
13 #endif\r
14 \r
15 /////////////////////////////////////////////////////////////////////////////\r
16 // CXkeymacsApp\r
17 \r
18 BEGIN_MESSAGE_MAP(CXkeymacsApp, CWinApp)\r
19         //{{AFX_MSG_MAP(CXkeymacsApp)\r
20                 // NOTE - the ClassWizard will add and remove mapping macros here.\r
21                 //    DO NOT EDIT what you see in these blocks of generated code!\r
22         //}}AFX_MSG_MAP\r
23 END_MESSAGE_MAP()\r
24 \r
25 /////////////////////////////////////////////////////////////////////////////\r
26 // CXkeymacsApp construction\r
27 \r
28 CXkeymacsApp::CXkeymacsApp()\r
29 {\r
30         m_hMutex = NULL;\r
31         m_bIsWow64 = FALSE;\r
32 }\r
33 \r
34 /////////////////////////////////////////////////////////////////////////////\r
35 // The one and only CXkeymacsApp object\r
36 \r
37 CXkeymacsApp theApp;\r
38 \r
39 /////////////////////////////////////////////////////////////////////////////\r
40 // CXkeymacsApp initialization\r
41 \r
42 BOOL CXkeymacsApp::InitInstance()\r
43 {\r
44 //      AfxEnableControlContainer();\r
45 \r
46         // Standard initialization\r
47         // If you are not using these features and wish to reduce the size\r
48         //  of your final executable, you should remove from the following\r
49         //  the specific initialization routines you do not need.\r
50         m_hMutex = CreateMutex(FALSE, 0, CString(MAKEINTRESOURCE(AFX_IDS_APP_TITLE)));\r
51         if (GetLastError() == ERROR_ALREADY_EXISTS) {\r
52                 CloseHandle(m_hMutex);\r
53                 m_hMutex = NULL;\r
54                 return FALSE;\r
55         }\r
56 \r
57         // Change the registry key under which our settings are stored.\r
58         SetRegistryKey(IDS_REGISTRY_KEY);\r
59 //\r
60 //      LoadStdProfileSettings();  // Load standard INI file options (including MRU)\r
61 \r
62         // Parse command line for standard shell commands, DDE, file open\r
63 //      CCommandLineInfo cmdInfo;\r
64 //      ParseCommandLine(cmdInfo);\r
65 \r
66         // Dispatch commands specified on the command line\r
67 //      if (!ProcessShellCommand(cmdInfo))\r
68 //              return FALSE;\r
69 \r
70         // The one and only window has been initialized, so show and update it.\r
71         m_pMainWnd = new CMainFrame;\r
72         m_pMainWnd->ShowWindow(SW_HIDE);\r
73         m_pMainWnd->UpdateWindow();\r
74         SetClassLongPtr(m_pMainWnd->m_hWnd, GCLP_HICON, (LONG_PTR)LoadIcon(IDR_MAINFRAME));\r
75 \r
76         // set registry key\r
77         CUtils::InitCUtils();\r
78         if (!Create64bitProcess())\r
79                 return FALSE;\r
80         CProfile::InitDllData();\r
81 \r
82         return TRUE;\r
83 }\r
84 \r
85 BOOL CXkeymacsApp::IsWow64()\r
86 {\r
87         return m_bIsWow64;\r
88 }\r
89 \r
90 BOOL CXkeymacsApp::Create64bitProcess()\r
91 {\r
92         typedef BOOL (WINAPI *PFIsWow64Process)(HANDLE, PBOOL);\r
93         PFIsWow64Process func = (PFIsWow64Process)GetProcAddress(GetModuleHandle(_T("kernel32")), _T("IsWow64Process"));\r
94         if (!func)\r
95                 return TRUE; // IsWow64Process not exists\r
96         if (!func(GetCurrentProcess(), &m_bIsWow64))\r
97                 return FALSE; // error\r
98         if (!m_bIsWow64)\r
99                 return TRUE; // do nothing\r
100         \r
101         TCHAR szFileName[MAX_PATH];\r
102         if (!GetModuleFileName(NULL, szFileName, sizeof(szFileName)))\r
103                 return FALSE;\r
104         TCHAR szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFile[_MAX_FNAME], szExt[_MAX_EXT];\r
105         if (_tsplitpath_s(szFileName, szDrive, szDir, szFile, szExt) ||\r
106                         _tcscat_s(szFile, _T("64")) ||\r
107                         _tmakepath_s(szFileName, szDrive, szDir, szFile, szExt))\r
108                 return FALSE;\r
109 \r
110         STARTUPINFO si;\r
111         ZeroMemory(&si, sizeof(si));\r
112         si.cb = sizeof(si);\r
113         PROCESS_INFORMATION pi;\r
114         ZeroMemory(&pi, sizeof(pi));\r
115         if (!CreateProcess(szFileName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))\r
116                 return FALSE;\r
117         // close unused handles\r
118         CloseHandle(pi.hProcess);\r
119         CloseHandle(pi.hThread);\r
120         return TRUE;\r
121 }\r
122 \r
123 BOOL CXkeymacsApp::SendIPCMessage(DWORD msg)\r
124 {\r
125         if (!m_bIsWow64)\r
126                 return TRUE;\r
127         DWORD ack, read;\r
128         return CallNamedPipe(IPC_PIPE, &msg, sizeof(msg), &ack, sizeof(DWORD), &read, NMPWAIT_WAIT_FOREVER);\r
129 }\r
130 \r
131 int CXkeymacsApp::ExitInstance() \r
132 {\r
133         if (m_hMutex) {\r
134                 ReleaseMutex(m_hMutex);\r
135                 CloseHandle(m_hMutex);\r
136 \r
137                 m_pMainWnd->DestroyWindow();\r
138                 delete m_pMainWnd;\r
139                 m_pMainWnd = NULL;\r
140         }\r
141 \r
142         return CWinApp::ExitInstance();\r
143 }\r