ReactOS 0.4.15-dev-7961-gdcf9eb0
ctfmon.cpp File Reference
#include "precomp.h"
#include "CRegWatcher.h"
#include "CLoaderWnd.h"
Include dependency graph for ctfmon.cpp:

Go to the source code of this file.

Typedefs

typedef BOOL(WINAPIFN_SetProcessShutdownParameters) (DWORD, DWORD)
 
typedef UINT(WINAPIFN_GetSystemWow64DirectoryA) (LPSTR, UINT)
 
typedef UINT(WINAPIFN_GetSystemWow64DirectoryW) (LPWSTR, UINT)
 

Functions

static VOID ParseCommandLine (_In_ LPCTSTR pszCmdLine)
 
static VOID WriteRegRun (VOID)
 
static HRESULT GetGlobalCompartment (_In_ REFGUID guid, _Inout_ ITfCompartment **ppComp)
 
static HRESULT SetGlobalCompartmentDWORD (_In_ REFGUID guid, _In_ DWORD dwValue)
 
static BOOL CheckX64System (_In_ LPTSTR lpCmdLine)
 
static BOOL InitApp (_In_ HINSTANCE hInstance, _In_ LPTSTR lpCmdLine)
 
VOID UninitApp (VOID)
 
static INT DoMainLoop (VOID)
 
EXTERN_C INT WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInst, LPTSTR lpCmdLine, INT nCmdShow)
 

Variables

FN_SetProcessShutdownParameters g_fnSetProcessShutdownParameters = NULL
 
FN_GetSystemWow64DirectoryA g_fnGetSystemWow64DirectoryA = NULL
 
FN_GetSystemWow64DirectoryW g_fnGetSystemWow64DirectoryW = NULL
 
HINSTANCE g_hInst = NULL
 
HINSTANCE g_hKernel32 = NULL
 
UINT g_uACP = CP_ACP
 
BOOL g_fWinLogon = FALSE
 
HANDLE g_hCicMutex = NULL
 
BOOL g_bOnWow64 = FALSE
 
BOOL g_fNoRunKey = FALSE
 
BOOL g_fJustRunKey = FALSE
 
DWORD g_dwOsInfo = 0
 
CLoaderWndg_pLoaderWnd = NULL
 

Typedef Documentation

◆ FN_GetSystemWow64DirectoryA

typedef UINT(WINAPI * FN_GetSystemWow64DirectoryA) (LPSTR, UINT)

Definition at line 17 of file ctfmon.cpp.

◆ FN_GetSystemWow64DirectoryW

typedef UINT(WINAPI * FN_GetSystemWow64DirectoryW) (LPWSTR, UINT)

Definition at line 20 of file ctfmon.cpp.

◆ FN_SetProcessShutdownParameters

typedef BOOL(WINAPI * FN_SetProcessShutdownParameters) (DWORD, DWORD)

Definition at line 13 of file ctfmon.cpp.

Function Documentation

◆ _tWinMain()

EXTERN_C INT WINAPI _tWinMain ( HINSTANCE  hInstance,
HINSTANCE  hPrevInst,
LPTSTR  lpCmdLine,
INT  nCmdShow 
)

Definition at line 324 of file ctfmon.cpp.

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}
HINSTANCE hInstance
Definition: charmap.c:19
static BOOL s_bUninitedSystem
Definition: CLoaderWnd.h:14
VOID UninitApp(VOID)
Definition: ctfmon.cpp:246
static INT DoMainLoop(VOID)
Definition: ctfmon.cpp:261
static VOID WriteRegRun(VOID)
Definition: ctfmon.cpp:73
CLoaderWnd * g_pLoaderWnd
Definition: ctfmon.cpp:32
static VOID ParseCommandLine(_In_ LPCTSTR pszCmdLine)
Definition: ctfmon.cpp:35
BOOL g_fJustRunKey
Definition: ctfmon.cpp:30
static BOOL InitApp(_In_ HINSTANCE hInstance, _In_ LPTSTR lpCmdLine)
Definition: ctfmon.cpp:188
#define NULL
Definition: types.h:112
EXTERN_C HRESULT WINAPI TF_UninitSystem(VOID)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
int32_t INT
Definition: typedefs.h:58
int ret

◆ CheckX64System()

static BOOL CheckX64System ( _In_ LPTSTR  lpCmdLine)
static

Definition at line 139 of file ctfmon.cpp.

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}
EXTERN_C HINSTANCE cicGetSystemModuleHandle(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir)
Definition: cicbase.cpp:130
FN_GetSystemWow64DirectoryA g_fnGetSystemWow64DirectoryA
Definition: ctfmon.cpp:18
HINSTANCE g_hKernel32
Definition: ctfmon.cpp:24
UINT(WINAPI * FN_GetSystemWow64DirectoryA)(LPSTR, UINT)
Definition: ctfmon.cpp:17
UINT(WINAPI * FN_GetSystemWow64DirectoryW)(LPWSTR, UINT)
Definition: ctfmon.cpp:20
FN_GetSystemWow64DirectoryW g_fnGetSystemWow64DirectoryW
Definition: ctfmon.cpp:21
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define GetProcAddress(x, y)
Definition: compat.h:753
#define MAX_PATH
Definition: compat.h:34
VOID WINAPI GetSystemInfo(IN LPSYSTEM_INFO lpSystemInfo)
Definition: sysinfo.c:143
#define FAILED(hr)
Definition: intsafe.h:51
#define TEXT(s)
Definition: k32.h:26
LPCWSTR szPath
Definition: env.c:37
static refpint_t pi[]
Definition: server.c:96
unsigned int UINT
Definition: ndis.h:50
#define PROCESSOR_ARCHITECTURE_IA64
Definition: ketypes.h:111
#define PROCESSOR_ARCHITECTURE_AMD64
Definition: ketypes.h:114
#define _countof(array)
Definition: sndvol32.h:68
#define StringCchCat
Definition: strsafe.h:317
WORD wShowWindow
Definition: winbase.h:843
WORD wProcessorArchitecture
Definition: winbase.h:1169
#define CreateProcess
Definition: winbase.h:3758
#define SW_SHOWMINNOACTIVE
Definition: winuser.h:777
char TCHAR
Definition: xmlstorage.h:189

Referenced by InitApp().

◆ DoMainLoop()

static INT DoMainLoop ( VOID  )
static

Definition at line 261 of file ctfmon.cpp.

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}
#define WATCHENTRY_MAX
Definition: CRegWatcher.h:17
#define msg(x)
Definition: auth_time.c:54
@ WI_DESKTOP_SWITCH
Definition: precomp.h:46
static HRESULT SetGlobalCompartmentDWORD(_In_ REFGUID guid, _In_ DWORD dwValue)
Definition: ctfmon.cpp:118
BOOL g_bOnWow64
Definition: ctfmon.cpp:28
#define INFINITE
Definition: serial.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
EXTERN_C const GUID GUID_COMPARTMENT_SPEECH_OPENCLOSE
Definition: msctf.idl:92
#define SYNCHRONIZE
Definition: nt_native.h:61
static VOID OnEvent(_In_ SIZE_T iEvent)
static HANDLE s_ahWatchEvents[WATCHENTRY_MAX]
Definition: CRegWatcher.h:21
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:714
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1829
#define CopyMemory
Definition: winbase.h:1710
#define WAIT_OBJECT_0
Definition: winbase.h:406
#define OpenEvent
Definition: winbase.h:3885
#define WM_QUIT
Definition: winuser.h:1623
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define QS_ALLINPUT
Definition: winuser.h:903
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

Referenced by _tWinMain().

◆ GetGlobalCompartment()

static HRESULT GetGlobalCompartment ( _In_ REFGUID  guid,
_Inout_ ITfCompartment **  ppComp 
)
static

Definition at line 98 of file ctfmon.cpp.

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}
#define E_FAIL
Definition: ddrawi.h:102
nsrefcnt Release()
const GUID * guid
EXTERN_C HRESULT WINAPI TF_GetGlobalCompartment(_Out_ ITfCompartmentMgr **ppCompMgr)
static nsIComponentManager * pCompMgr
Definition: nsembed.c:62
HRESULT hr
Definition: shlfolder.c:183

Referenced by SetGlobalCompartmentDWORD().

◆ InitApp()

static BOOL InitApp ( _In_ HINSTANCE  hInstance,
_In_ LPTSTR  lpCmdLine 
)
static

Definition at line 188 of file ctfmon.cpp.

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}
EXTERN_C BOOL cicIsWow64(VOID)
Definition: cicbase.cpp:58
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
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
HANDLE g_hCicMutex
Definition: ctfmon.cpp:27
DWORD g_dwOsInfo
Definition: ctfmon.cpp:31
FN_SetProcessShutdownParameters g_fnSetProcessShutdownParameters
Definition: ctfmon.cpp:14
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
EXTERN_C HANDLE WINAPI TF_CreateCicLoadMutex(_Out_ LPBOOL pfWinLogon)
EXTERN_C HRESULT WINAPI TF_InitSystem(VOID)
EXTERN_C BOOL WINAPI GetPopupTipbar(HWND hWnd, BOOL fWinLogon)
Definition: msutb.cpp:6336
static BOOL Init()
Definition: CRegWatcher.cpp:40
#define SHUTDOWN_NORETRY
Definition: winbase.h:448
#define SWP_NOACTIVATE
Definition: winuser.h:1242
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 SWP_NOSIZE
Definition: winuser.h:1245
#define HWND_BOTTOM
Definition: winuser.h:1205

Referenced by _tWinMain().

◆ ParseCommandLine()

static VOID ParseCommandLine ( _In_ LPCTSTR  pszCmdLine)
static

Definition at line 35 of file ctfmon.cpp.

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}
BOOL g_fNoRunKey
Definition: ctfmon.cpp:29
#define pch(ap)
Definition: match.c:418
#define UNICODE_NULL
const CHAR * LPCTSTR
Definition: xmlstorage.h:193

Referenced by _tWinMain().

◆ SetGlobalCompartmentDWORD()

static HRESULT SetGlobalCompartmentDWORD ( _In_ REFGUID  guid,
_In_ DWORD  dwValue 
)
static

Definition at line 118 of file ctfmon.cpp.

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}
static HRESULT GetGlobalCompartment(_In_ REFGUID guid, _Inout_ ITfCompartment **ppComp)
Definition: ctfmon.cpp:98
@ VT_I4
Definition: compat.h:2298
HRESULT SetValue([in] TfClientId tid, [in] const VARIANT *pvarValue)
ULONG Release()
#define V_VT(A)
Definition: oleauto.h:211
#define V_I4(A)
Definition: oleauto.h:247

Referenced by DoMainLoop().

◆ UninitApp()

VOID UninitApp ( VOID  )

Definition at line 246 of file ctfmon.cpp.

247{
248 // Close Tipbar Popup
250
251 // Close the mutex
254
255 // Quit watching registry if x86/x64 native
256 if (!g_bOnWow64)
258}
EXTERN_C VOID WINAPI ClosePopupTipbar(VOID)
Definition: msutb.cpp:6377
static VOID Uninit()
Definition: CRegWatcher.cpp:64

Referenced by _tWinMain(), and CLoaderWnd::WindowProc().

◆ WriteRegRun()

static VOID WriteRegRun ( VOID  )
static

Definition at line 73 of file ctfmon.cpp.

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}
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
FxAutoRegKey hKey
#define REG_SZ
Definition: layer.c:22
#define error(str)
Definition: mkdosfs.c:1605
#define HKEY_CURRENT_USER
Definition: winreg.h:11
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKey
Definition: winreg.h:500
unsigned char BYTE
Definition: xxhash.c:193

Referenced by _tWinMain(), and InitApp().

Variable Documentation

◆ g_bOnWow64

BOOL g_bOnWow64 = FALSE

Definition at line 28 of file ctfmon.cpp.

Referenced by DoMainLoop(), InitApp(), UninitApp(), and CLoaderWnd::WindowProc().

◆ g_dwOsInfo

DWORD g_dwOsInfo = 0

Definition at line 31 of file ctfmon.cpp.

Referenced by CRegWatcher::Init(), InitApp(), and CLoaderWnd::WindowProc().

◆ g_fJustRunKey

BOOL g_fJustRunKey = FALSE

Definition at line 30 of file ctfmon.cpp.

Referenced by _tWinMain(), and ParseCommandLine().

◆ g_fnGetSystemWow64DirectoryA

FN_GetSystemWow64DirectoryA g_fnGetSystemWow64DirectoryA = NULL

Definition at line 18 of file ctfmon.cpp.

Referenced by CheckX64System().

◆ g_fnGetSystemWow64DirectoryW

FN_GetSystemWow64DirectoryW g_fnGetSystemWow64DirectoryW = NULL

Definition at line 21 of file ctfmon.cpp.

Referenced by CheckX64System().

◆ g_fNoRunKey

BOOL g_fNoRunKey = FALSE

Definition at line 29 of file ctfmon.cpp.

Referenced by ParseCommandLine(), and WriteRegRun().

◆ g_fnSetProcessShutdownParameters

FN_SetProcessShutdownParameters g_fnSetProcessShutdownParameters = NULL

Definition at line 14 of file ctfmon.cpp.

Referenced by InitApp().

◆ g_fWinLogon

BOOL g_fWinLogon = FALSE

Definition at line 26 of file ctfmon.cpp.

Referenced by InitApp(), and CLoaderWnd::WindowProc().

◆ g_hCicMutex

HANDLE g_hCicMutex = NULL

Definition at line 27 of file ctfmon.cpp.

Referenced by InitApp(), and UninitApp().

◆ g_hInst

◆ g_hKernel32

HINSTANCE g_hKernel32 = NULL

Definition at line 24 of file ctfmon.cpp.

Referenced by CheckX64System(), and InitApp().

◆ g_pLoaderWnd

CLoaderWnd* g_pLoaderWnd = NULL

Definition at line 32 of file ctfmon.cpp.

Referenced by _tWinMain(), and InitApp().

◆ g_uACP

UINT g_uACP = CP_ACP

Definition at line 25 of file ctfmon.cpp.

Referenced by InitApp().