ReactOS 0.4.15-dev-8002-gbbb3b00
ctfmon.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS CTF Monitor
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Providing Language Bar front-end
5 * COPYRIGHT: Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 */
7
8#include "precomp.h"
9#include "CRegWatcher.h"
10#include "CLoaderWnd.h"
11
12// kernel32!SetProcessShutdownParameters
15
16// kernel32!GetSystemWow64DirectoryA
19// kernel32!GetSystemWow64DirectoryW
22
23HINSTANCE g_hInst = NULL; // The application instance
24HINSTANCE g_hKernel32 = NULL; // The "kernel32.dll" instance
25UINT g_uACP = CP_ACP; // The active codepage
26BOOL g_fWinLogon = FALSE; // Is it a log-on process?
27HANDLE g_hCicMutex = NULL; // The Cicero mutex
28BOOL g_bOnWow64 = FALSE; // Is the app running on WoW64?
29BOOL g_fNoRunKey = FALSE; // Don't write registry key "Run"?
30BOOL g_fJustRunKey = FALSE; // Just write registry key "Run"?
31DWORD g_dwOsInfo = 0; // The OS version info. See cicGetOSInfo
32CLoaderWnd* g_pLoaderWnd = NULL; // Tipbar loader window
33
34static VOID
36 _In_ LPCTSTR pszCmdLine)
37{
39
40 for (LPCTSTR pch = pszCmdLine; *pch; ++pch)
41 {
42 // Skip space
43 while (*pch == TEXT(' '))
44 ++pch;
45
46 if (*pch == TEXT('\0'))
47 return;
48
49 if ((*pch == TEXT('-')) || (*pch == TEXT('/')))
50 {
51 ++pch;
52 switch (*pch)
53 {
54 case TEXT('N'): case TEXT('n'): // Found "/N" option
56 break;
57
58 case TEXT('R'): case TEXT('r'): // Found "/R" option
60 break;
61
62 case UNICODE_NULL:
63 return;
64
65 default:
66 break;
67 }
68 }
69 }
70}
71
72static VOID
74{
75 if (g_fNoRunKey) // If "/N" option is specified
76 return; // Don't write
77
78 // Open "Run" key
79 HKEY hKey;
81 TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
82 &hKey);
83 if (error != ERROR_SUCCESS)
84 return;
85
86 // Write the module path
87 CicSystemModulePath ModPath;
88 if (ModPath.Init(TEXT("ctfmon.exe"), FALSE))
89 {
90 DWORD cbData = (ModPath.m_cchPath + 1) * sizeof(TCHAR);
91 ::RegSetValueEx(hKey, TEXT("ctfmon.exe"), 0, REG_SZ, (BYTE*)ModPath.m_szPath, cbData);
92 }
93
95}
96
97static HRESULT
100 _Inout_ ITfCompartment **ppComp)
101{
102 *ppComp = NULL;
103
106 if (FAILED(hr))
107 return hr;
108
109 if (!pCompMgr)
110 return E_FAIL;
111
112 hr = pCompMgr->GetCompartment(guid, ppComp);
113 pCompMgr->Release();
114 return hr;
115}
116
117static HRESULT
120 _In_ DWORD dwValue)
121{
122 HRESULT hr;
123 VARIANT vari;
124 ITfCompartment *pComp;
125
126 hr = GetGlobalCompartment(guid, &pComp);
127 if (FAILED(hr))
128 return hr;
129
130 V_VT(&vari) = VT_I4;
131 V_I4(&vari) = dwValue;
132 hr = pComp->SetValue(0, &vari);
133
134 pComp->Release();
135 return hr;
136}
137
138static BOOL
140 _In_ LPTSTR lpCmdLine)
141{
142 // Is the system x64?
143 SYSTEM_INFO SystemInfo;
144 ::GetSystemInfo(&SystemInfo);
147 {
148 return FALSE;
149 }
150
151 // Get GetSystemWow64DirectoryW function
153#ifdef UNICODE
155 (FN_GetSystemWow64DirectoryW)::GetProcAddress(g_hKernel32, "GetSystemWow64DirectoryW");
157 return FALSE;
158#else
160 (FN_GetSystemWow64DirectoryA)::GetProcAddress(g_hKernel32, "GetSystemWow64DirectoryA");
162 return FALSE;
163#endif
164
165 // Build WoW64 ctfmon.exe pathname
167#ifdef UNICODE
169#else
171#endif
172 if (!cchPath && FAILED(StringCchCat(szPath, _countof(szPath), TEXT("\\ctfmon.exe"))))
173 return FALSE;
174
175 // Create a WoW64 ctfmon.exe process
177 STARTUPINFO si = { sizeof(si) };
179 if (!::CreateProcess(szPath, lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
180 return FALSE;
181
182 ::CloseHandle(pi.hThread);
183 ::CloseHandle(pi.hProcess);
184 return TRUE;
185}
186
187static BOOL
190 _In_ LPTSTR lpCmdLine)
191{
192 g_hInst = hInstance; // Save the instance handle
193
194 g_bOnWow64 = cicIsWow64(); // Is the current process on WoW64?
195 cicGetOSInfo(&g_uACP, &g_dwOsInfo); // Get OS info
196
197 // Create a mutex for Cicero
199 if (!g_hCicMutex)
200 return FALSE;
201
202 // Write to "Run" registry key for starting up
203 WriteRegRun();
204
205 // Call SetProcessShutdownParameters if possible
207 {
211 ::GetProcAddress(g_hKernel32, "SetProcessShutdownParameters");
214 }
215
216 // Start text framework
218
219 // Start watching registry if x86/x64 native
220 if (!g_bOnWow64)
222
223 // Create Tipbar loader window
225 if (!g_pLoaderWnd || !g_pLoaderWnd->Init())
226 return FALSE;
227
228 if (g_pLoaderWnd->CreateWnd())
229 {
230 // Go to the bottom of the hell
233 }
234
235 // Display Tipbar Popup if x86/x64 native and necessary
236 if (!g_bOnWow64)
238
239 // Do x64 stuffs
240 CheckX64System(lpCmdLine);
241
242 return TRUE;
243}
244
245VOID
247{
248 // Close Tipbar Popup
250
251 // Close the mutex
254
255 // Quit watching registry if x86/x64 native
256 if (!g_bOnWow64)
258}
259
260static INT
262{
263 MSG msg;
264
265 if (g_bOnWow64) // Is the current process on WoW64?
266 {
267 // Just a simple message loop
268 while (::GetMessage(&msg, NULL, 0, 0))
269 {
272 }
273 return (INT)msg.wParam;
274 }
275
276 // Open the existing event by the name
277 HANDLE hSwitchEvent = ::OpenEvent(SYNCHRONIZE, FALSE, TEXT("WinSta0_DesktopSwitch"));
278
279 // The target events to watch
280 HANDLE ahEvents[WATCHENTRY_MAX + 1];
281
282 // Borrow some handles from CRegWatcher
284
285 ahEvents[WI_DESKTOP_SWITCH] = hSwitchEvent; // Add it
286
287 // Another message loop
288 for (;;)
289 {
290 // Wait for target signal
291 DWORD dwWait = ::MsgWaitForMultipleObjects(_countof(ahEvents), ahEvents, 0, INFINITE,
293 if (dwWait == (WAIT_OBJECT_0 + _countof(ahEvents))) // Is input available?
294 {
295 // Do the events
296 while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
297 {
298 if (msg.message == WM_QUIT)
299 goto Quit;
300
303 }
304 }
305 else if (dwWait == (WAIT_OBJECT_0 + WI_DESKTOP_SWITCH)) // Desktop switch?
306 {
308 ::ResetEvent(hSwitchEvent);
309 }
310 else // Do the other events
311 {
313 }
314 }
315
316Quit:
317 ::CloseHandle(hSwitchEvent);
318
319 return (INT)msg.wParam;
320}
321
322// The main function for Unicode Win32
326 HINSTANCE hPrevInst,
327 LPTSTR lpCmdLine,
328 INT nCmdShow)
329{
330 UNREFERENCED_PARAMETER(hPrevInst);
331 UNREFERENCED_PARAMETER(nCmdShow);
332
333 // Parse command line
334 ParseCommandLine(lpCmdLine);
335
336 if (g_fJustRunKey) // If "/R" option is specified
337 {
338 // Just write registry and exit
339 WriteRegRun();
340 return 1;
341 }
342
343 // Initialize the application
344 if (!InitApp(hInstance, lpCmdLine))
345 return 0;
346
347 // The main loop
348 INT ret = DoMainLoop();
349
350 // Clean up the loader
351 if (g_pLoaderWnd)
352 {
353 delete g_pLoaderWnd;
355 }
356
357 // Un-initialize app and text framework
359 {
360 UninitApp();
362 }
363
364 return ret;
365}
#define WATCHENTRY_MAX
Definition: CRegWatcher.h:17
#define msg(x)
Definition: auth_time.c:54
@ WI_DESKTOP_SWITCH
Definition: precomp.h:46
#define EXTERN_C
Definition: basetyps.h:12
#define RegCloseKey(hKey)
Definition: registry.h:49
HINSTANCE hInstance
Definition: charmap.c:19
EXTERN_C BOOL cicIsWow64(VOID)
Definition: cicbase.cpp:58
EXTERN_C HINSTANCE cicGetSystemModuleHandle(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir)
Definition: cicbase.cpp:130
EXTERN_C void cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo)
Definition: cicbase.cpp:82
#define cicNoThrow
Definition: cicbase.h:34
#define CIC_OSINFO_NT
Definition: cicbase.h:44
static BOOL s_bUninitedSystem
Definition: CLoaderWnd.h:14
HWND m_hWnd
Definition: CLoaderWnd.h:13
BOOL Init()
Definition: CLoaderWnd.cpp:15
HWND CreateWnd()
Definition: CLoaderWnd.cpp:36
static BOOL CheckX64System(_In_ LPTSTR lpCmdLine)
Definition: ctfmon.cpp:139
static HRESULT SetGlobalCompartmentDWORD(_In_ REFGUID guid, _In_ DWORD dwValue)
Definition: ctfmon.cpp:118
BOOL g_fNoRunKey
Definition: ctfmon.cpp:29
VOID UninitApp(VOID)
Definition: ctfmon.cpp:246
HANDLE g_hCicMutex
Definition: ctfmon.cpp:27
BOOL g_bOnWow64
Definition: ctfmon.cpp:28
static INT DoMainLoop(VOID)
Definition: ctfmon.cpp:261
FN_GetSystemWow64DirectoryA g_fnGetSystemWow64DirectoryA
Definition: ctfmon.cpp:18
static HRESULT GetGlobalCompartment(_In_ REFGUID guid, _Inout_ ITfCompartment **ppComp)
Definition: ctfmon.cpp:98
static VOID WriteRegRun(VOID)
Definition: ctfmon.cpp:73
DWORD g_dwOsInfo
Definition: ctfmon.cpp:31
HINSTANCE g_hKernel32
Definition: ctfmon.cpp:24
UINT(WINAPI * FN_GetSystemWow64DirectoryA)(LPSTR, UINT)
Definition: ctfmon.cpp:17
CLoaderWnd * g_pLoaderWnd
Definition: ctfmon.cpp:32
static VOID ParseCommandLine(_In_ LPCTSTR pszCmdLine)
Definition: ctfmon.cpp:35
UINT(WINAPI * FN_GetSystemWow64DirectoryW)(LPWSTR, UINT)
Definition: ctfmon.cpp:20
FN_SetProcessShutdownParameters g_fnSetProcessShutdownParameters
Definition: ctfmon.cpp:14
FN_GetSystemWow64DirectoryW g_fnGetSystemWow64DirectoryW
Definition: ctfmon.cpp:21
BOOL g_fJustRunKey
Definition: ctfmon.cpp:30
static BOOL InitApp(_In_ HINSTANCE hInstance, _In_ LPTSTR lpCmdLine)
Definition: ctfmon.cpp:188
BOOL g_fWinLogon
Definition: ctfmon.cpp:26
UINT g_uACP
Definition: ctfmon.cpp:25
BOOL(WINAPI * FN_SetProcessShutdownParameters)(DWORD, DWORD)
Definition: ctfmon.cpp:13
HINSTANCE g_hInst
Definition: ctfmon.cpp:23
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define CP_ACP
Definition: compat.h:109
#define GetProcAddress(x, y)
Definition: compat.h:753
#define MAX_PATH
Definition: compat.h:34
@ VT_I4
Definition: compat.h:2298
VOID WINAPI GetSystemInfo(IN LPSYSTEM_INFO lpSystemInfo)
Definition: sysinfo.c:143
#define INFINITE
Definition: serial.h:102
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
#define _tWinMain
Definition: tchar.h:498
HRESULT SetValue([in] TfClientId tid, [in] const VARIANT *pvarValue)
ULONG Release()
nsrefcnt Release()
#define FAILED(hr)
Definition: intsafe.h:51
#define TEXT(s)
Definition: k32.h:26
#define REG_SZ
Definition: layer.c:22
const GUID * guid
#define error(str)
Definition: mkdosfs.c:1605
#define pch(ap)
Definition: match.c:418
LPCWSTR szPath
Definition: env.c:37
static refpint_t pi[]
Definition: server.c:96
#define _Inout_
Definition: ms_sal.h:378
#define _In_
Definition: ms_sal.h:308
EXTERN_C HRESULT WINAPI TF_GetGlobalCompartment(_Out_ ITfCompartmentMgr **ppCompMgr)
EXTERN_C HRESULT WINAPI TF_UninitSystem(VOID)
EXTERN_C const GUID GUID_COMPARTMENT_SPEECH_OPENCLOSE
Definition: msctf.idl:92
EXTERN_C HANDLE WINAPI TF_CreateCicLoadMutex(_Out_ LPBOOL pfWinLogon)
EXTERN_C HRESULT WINAPI TF_InitSystem(VOID)
EXTERN_C VOID WINAPI ClosePopupTipbar(VOID)
Definition: msutb.cpp:6377
EXTERN_C BOOL WINAPI GetPopupTipbar(HWND hWnd, BOOL fWinLogon)
Definition: msutb.cpp:6336
unsigned int UINT
Definition: ndis.h:50
#define PROCESSOR_ARCHITECTURE_IA64
Definition: ketypes.h:111
#define PROCESSOR_ARCHITECTURE_AMD64
Definition: ketypes.h:114
static nsIComponentManager * pCompMgr
Definition: nsembed.c:62
#define BOOL
Definition: nt_native.h:43
#define SYNCHRONIZE
Definition: nt_native.h:61
#define DWORD
Definition: nt_native.h:44
#define UNICODE_NULL
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define V_VT(A)
Definition: oleauto.h:211
#define V_I4(A)
Definition: oleauto.h:247
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: sndvol32.h:68
#define StringCchCat
Definition: strsafe.h:317
static BOOL Init()
Definition: CRegWatcher.cpp:40
static VOID OnEvent(_In_ SIZE_T iEvent)
static VOID Uninit()
Definition: CRegWatcher.cpp:64
static HANDLE s_ahWatchEvents[WATCHENTRY_MAX]
Definition: CRegWatcher.h:21
Definition: scsiwmi.h:51
WORD wShowWindow
Definition: winbase.h:843
WORD wProcessorArchitecture
Definition: winbase.h:1169
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:714
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1829
int32_t INT
Definition: typedefs.h:58
int ret
#define CreateProcess
Definition: winbase.h:3758
#define SHUTDOWN_NORETRY
Definition: winbase.h:448
#define CopyMemory
Definition: winbase.h:1710
#define WAIT_OBJECT_0
Definition: winbase.h:406
#define OpenEvent
Definition: winbase.h:3885
#define WINAPI
Definition: msvc.h:6
#define HKEY_CURRENT_USER
Definition: winreg.h:11
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKey
Definition: winreg.h:500
#define SWP_NOACTIVATE
Definition: winuser.h:1242
#define WM_QUIT
Definition: winuser.h:1623
BOOL WINAPI TranslateMessage(_In_ const MSG *)
BOOL WINAPI SetWindowPos(_In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
#define SWP_NOMOVE
Definition: winuser.h:1244
#define QS_ALLINPUT
Definition: winuser.h:903
#define SWP_NOSIZE
Definition: winuser.h:1245
DWORD WINAPI MsgWaitForMultipleObjects(_In_ DWORD nCount, _In_reads_opt_(nCount) CONST HANDLE *pHandles, _In_ BOOL fWaitAll, _In_ DWORD dwMilliseconds, _In_ DWORD dwWakeMask)
#define GetMessage
Definition: winuser.h:5790
#define PM_REMOVE
Definition: winuser.h:1196
#define PeekMessage
Definition: winuser.h:5830
#define DispatchMessage
Definition: winuser.h:5765
#define SW_SHOWMINNOACTIVE
Definition: winuser.h:777
#define HWND_BOTTOM
Definition: winuser.h:1205
char TCHAR
Definition: xmlstorage.h:189
char * LPSTR
Definition: xmlstorage.h:182
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192
unsigned char BYTE
Definition: xxhash.c:193