OSDN Git Service

Give main roles to 32bit version and simplify 64bit version.
[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 #ifdef _AFXDLL\r
58         Enable3dControls();                     // Call this when using MFC in a shared DLL\r
59 #else\r
60         Enable3dControlsStatic();       // Call this when linking to MFC statically\r
61 #endif\r
62 \r
63         // Change the registry key under which our settings are stored.\r
64         SetRegistryKey(IDS_REGISTRY_KEY);\r
65 //\r
66 //      LoadStdProfileSettings();  // Load standard INI file options (including MRU)\r
67 \r
68         // Parse command line for standard shell commands, DDE, file open\r
69 //      CCommandLineInfo cmdInfo;\r
70 //      ParseCommandLine(cmdInfo);\r
71 \r
72         // Dispatch commands specified on the command line\r
73 //      if (!ProcessShellCommand(cmdInfo))\r
74 //              return FALSE;\r
75 \r
76         // The one and only window has been initialized, so show and update it.\r
77         m_pMainWnd = new CMainFrame;\r
78         m_pMainWnd->ShowWindow(SW_HIDE);\r
79         m_pMainWnd->UpdateWindow();\r
80         SetClassLongPtr(m_pMainWnd->m_hWnd, GCLP_HICON, (LONG_PTR)LoadIcon(IDR_MAINFRAME));\r
81 \r
82         // set registry key\r
83         CUtils::InitCUtils();\r
84         if (!Create64bitProcess())\r
85                 return FALSE;\r
86         CProfile::InitDllData();\r
87 \r
88         return TRUE;\r
89 }\r
90 \r
91 BOOL CXkeymacsApp::IsWow64()\r
92 {\r
93         return m_bIsWow64;\r
94 }\r
95 \r
96 BOOL CXkeymacsApp::Create64bitProcess()\r
97 {\r
98         typedef BOOL (WINAPI *PFIsWow64Process)(HANDLE, PBOOL);\r
99         PFIsWow64Process func = (PFIsWow64Process)GetProcAddress(GetModuleHandle(_T("kernel32")), _T("IsWow64Process"));\r
100         if (!func)\r
101                 return TRUE; // IsWow64Process not exists\r
102         if (!func(GetCurrentProcess(), &m_bIsWow64))\r
103                 return FALSE; // error\r
104         if (!m_bIsWow64)\r
105                 return TRUE; // do nothing\r
106         \r
107         TCHAR szFileName[MAX_PATH];\r
108         if (!GetModuleFileName(NULL, szFileName, sizeof(szFileName)))\r
109                 return FALSE;\r
110         TCHAR szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFile[_MAX_FNAME], szExt[_MAX_EXT];\r
111         if (_tsplitpath_s(szFileName, szDrive, szDir, szFile, szExt) ||\r
112                         _tcscat_s(szFile, _T("64")) ||\r
113                         _tmakepath_s(szFileName, szDrive, szDir, szFile, szExt))\r
114                 return FALSE;\r
115 \r
116         STARTUPINFO si;\r
117         ZeroMemory(&si, sizeof(si));\r
118         si.cb = sizeof(si);\r
119         PROCESS_INFORMATION pi;\r
120         ZeroMemory(&pi, sizeof(pi));\r
121         if (!CreateProcess(szFileName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))\r
122                 return FALSE;\r
123         // close unused handles\r
124         CloseHandle(pi.hProcess);\r
125         CloseHandle(pi.hThread);\r
126         return TRUE;\r
127 }\r
128 \r
129 BOOL CXkeymacsApp::SendIPCMessage(DWORD msg)\r
130 {\r
131         if (!m_bIsWow64)\r
132                 return TRUE;\r
133         DWORD ack, read;\r
134         return CallNamedPipe(IPC_PIPE, &msg, sizeof(msg), &ack, sizeof(DWORD), &read, 10000);\r
135 }\r
136 \r
137 int CXkeymacsApp::ExitInstance() \r
138 {\r
139         if (m_hMutex) {\r
140                 ReleaseMutex(m_hMutex);\r
141                 CloseHandle(m_hMutex);\r
142 \r
143                 m_pMainWnd->DestroyWindow();\r
144                 delete m_pMainWnd;\r
145                 m_pMainWnd = NULL;\r
146         }\r
147 \r
148         return CWinApp::ExitInstance();\r
149 }\r