ReactOS 0.4.15-dev-8039-g69ebfd6
cicbase.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Cicero
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Cicero base
5 * COPYRIGHT: Copyright 2023-2024 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 */
7
8#include "precomp.h"
9#include "cicbase.h"
10#include <shlwapi.h>
11#include <stdlib.h>
12#include <string.h>
13#include <tchar.h>
14#include <strsafe.h>
15
16void* operator new(size_t size, const CicNoThrow&) noexcept
17{
18 return cicMemAllocClear(size);
19}
20void* operator new[](size_t size, const CicNoThrow&) noexcept
21{
22 return cicMemAllocClear(size);
23}
24void operator delete(void* ptr) noexcept
25{
27}
28void operator delete[](void* ptr) noexcept
29{
31}
32void operator delete(void* ptr, size_t size) noexcept
33{
35}
36void operator delete[](void* ptr, size_t size) noexcept
37{
39}
40
41// FIXME
43{
50
51// FIXME
52typedef LONG NTSTATUS;
53
54/* ntdll!NtQueryInformationProcess */
56
59{
60 static FN_NtQueryInformationProcess s_fnNtQueryInformationProcess = NULL;
62
63 if (!s_fnNtQueryInformationProcess)
64 {
65 HMODULE hNTDLL = cicGetSystemModuleHandle(TEXT("ntdll.dll"), FALSE);
66 if (!hNTDLL)
67 return FALSE;
68
69 s_fnNtQueryInformationProcess =
70 (FN_NtQueryInformationProcess)GetProcAddress(hNTDLL, "NtQueryInformationProcess");
71 if (!s_fnNtQueryInformationProcess)
72 return FALSE;
73 }
74
75 Value = 0;
76 s_fnNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information,
77 &Value, sizeof(Value), NULL);
78 return !!Value;
79}
80
82void cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo)
83{
84 *pdwOSInfo = 0;
85
86 /* Check OS version info */
87 OSVERSIONINFO VerInfo;
88 VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
89 GetVersionEx(&VerInfo);
90 if (VerInfo.dwPlatformId == DLLVER_PLATFORM_NT)
91 {
92 *pdwOSInfo |= CIC_OSINFO_NT;
93 if (VerInfo.dwMajorVersion >= 5)
94 {
95 *pdwOSInfo |= CIC_OSINFO_2KPLUS;
96 if (VerInfo.dwMinorVersion > 0)
97 *pdwOSInfo |= CIC_OSINFO_XPPLUS;
98 }
99 }
100 else
101 {
102 if (VerInfo.dwMinorVersion >= 10)
103 *pdwOSInfo |= CIC_OSINFO_98PLUS;
104 else
105 *pdwOSInfo |= CIC_OSINFO_95;
106 }
107
108 /* Check codepage */
109 *puACP = GetACP();
110 switch (*puACP)
111 {
112 case 932: /* Japanese (Japan) */
113 case 936: /* Chinese (PRC, Singapore) */
114 case 949: /* Korean (Korea) */
115 case 950: /* Chinese (Taiwan, Hong Kong) */
116 *pdwOSInfo |= CIC_OSINFO_CJK;
117 break;
118 }
119
120 if (GetSystemMetrics(SM_IMMENABLED))
121 *pdwOSInfo |= CIC_OSINFO_IMM;
122
124 *pdwOSInfo |= CIC_OSINFO_DBCS;
125}
126
127// Get an instance handle that is already loaded
132 _In_ BOOL bSysWinDir)
133{
134 CicSystemModulePath ModPath;
135 if (!ModPath.Init(pszFileName, bSysWinDir))
136 return NULL;
137 return GetModuleHandle(ModPath.m_szPath);
138}
139
140// Load a system library
145 _In_ BOOL bSysWinDir)
146{
147 CicSystemModulePath ModPath;
148 if (!ModPath.Init(pszFileName, bSysWinDir))
149 return NULL;
150 return ::LoadLibrary(ModPath.m_szPath);
151}
152
153BOOL
154CicSystemModulePath::Init(
156 _In_ BOOL bSysWinDir)
157{
158 SIZE_T cchPath;
159 if (bSysWinDir)
160 {
161 // Usually C:\Windows or C:\ReactOS
162 cchPath = ::GetSystemWindowsDirectory(m_szPath, _countof(m_szPath));
163 }
164 else
165 {
166 // Usually C:\Windows\system32 or C:\ReactOS\system32
167 cchPath = ::GetSystemDirectory(m_szPath, _countof(m_szPath));
168 }
169
170 m_szPath[_countof(m_szPath) - 1] = TEXT('\0'); // Avoid buffer overrun
171
172 if ((cchPath == 0) || (cchPath > _countof(m_szPath) - 2))
173 goto Failure;
174
175 // Add backslash if necessary
176 if ((cchPath > 0) && (m_szPath[cchPath - 1] != TEXT('\\')))
177 {
178 m_szPath[cchPath + 0] = TEXT('\\');
179 m_szPath[cchPath + 1] = TEXT('\0');
180 }
181
182 // Append pszFileName
183 if (FAILED(StringCchCat(m_szPath, _countof(m_szPath), pszFileName)))
184 goto Failure;
185
186 m_cchPath = _tcslen(m_szPath);
187 return TRUE;
188
189Failure:
190 m_szPath[0] = UNICODE_NULL;
191 m_cchPath = 0;
192 return FALSE;
193}
194
197{
198 static FN_CoCreateInstance s_fn = NULL;
199 if (fnUserCoCreateInstance)
200 s_fn = fnUserCoCreateInstance;
201 return s_fn;
202}
203
207 _In_ REFCLSID rclsid,
208 _In_ LPUNKNOWN pUnkOuter,
209 _In_ DWORD dwClsContext,
210 _In_ REFIID iid,
212{
213 static HINSTANCE s_hOle32 = NULL;
214 static FN_CoCreateInstance s_fnCoCreateInstance = NULL;
215 if (!s_fnCoCreateInstance)
216 {
217 if (!s_hOle32)
218 s_hOle32 = cicLoadSystemLibrary(TEXT("ole32.dll"), FALSE);
219 s_fnCoCreateInstance = (FN_CoCreateInstance)GetProcAddress(s_hOle32, "CoCreateInstance");
220 if (!s_fnCoCreateInstance)
221 return E_NOTIMPL;
222 }
223
224 return s_fnCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
225}
226
232 _In_ REFCLSID rclsid,
233 _In_ LPUNKNOWN pUnkOuter,
234 _In_ DWORD dwClsContext,
235 _In_ REFIID iid,
237{
238 // NOTE: It looks like Cicero wants to hook CoCreateInstance
240 if (fnUserCoCreateInstance)
241 return fnUserCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
242
243 return cicRealCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
244}
245
250BOOL
252{
253 if (fnCoCreateInstance)
254 _cicGetSetUserCoCreateInstance(fnCoCreateInstance);
255 return TRUE;
256}
#define EXTERN_C
Definition: basetyps.h:12
enum _PROCESSINFOCLASS PROCESSINFOCLASS
LONG NTSTATUS
Definition: cicbase.cpp:52
EXTERN_C BOOL cicIsWow64(VOID)
Definition: cicbase.cpp:58
EXTERN_C HINSTANCE cicLoadSystemLibrary(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir)
Definition: cicbase.cpp:143
NTSTATUS(WINAPI * FN_NtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG)
Definition: cicbase.cpp:55
EXTERN_C HRESULT cicRealCoCreateInstance(_In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv)
Definition: cicbase.cpp:206
@ ProcessDebugPort
Definition: cicbase.cpp:45
@ ProcessBreakOnTermination
Definition: cicbase.cpp:48
@ ProcessBasicInformation
Definition: cicbase.cpp:44
@ ProcessWow64Information
Definition: cicbase.cpp:46
@ ProcessImageFileName
Definition: cicbase.cpp:47
HRESULT cicCoCreateInstance(_In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv)
Definition: cicbase.cpp:231
static FN_CoCreateInstance _cicGetSetUserCoCreateInstance(FN_CoCreateInstance fnUserCoCreateInstance)
Definition: cicbase.cpp:196
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
EXTERN_C BOOL TFInitLib(FN_CoCreateInstance fnCoCreateInstance)
Definition: cicbase.cpp:251
#define CIC_OSINFO_IMM
Definition: cicbase.h:49
static void cicMemFree(LPVOID ptr)
Definition: cicbase.h:27
#define CIC_OSINFO_NT
Definition: cicbase.h:44
#define CIC_OSINFO_DBCS
Definition: cicbase.h:50
HRESULT(WINAPI * FN_CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
Definition: cicbase.h:125
#define CIC_OSINFO_2KPLUS
Definition: cicbase.h:45
#define CIC_OSINFO_XPPLUS
Definition: cicbase.h:51
#define CIC_OSINFO_95
Definition: cicbase.h:46
static LPVOID cicMemAllocClear(SIZE_T size)
Definition: cicbase.h:15
#define CIC_OSINFO_CJK
Definition: cicbase.h:48
#define CIC_OSINFO_98PLUS
Definition: cicbase.h:47
#define E_NOTIMPL
Definition: ddrawi.h:99
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GetProcAddress(x, y)
Definition: compat.h:753
#define GetCurrentProcess()
Definition: compat.h:759
UINT WINAPI GetACP(void)
Definition: locale.c:2020
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizeiptr size
Definition: glext.h:5919
_PROCESSINFOCLASS
Definition: winternl.h:393
REFIID LPVOID * ppv
Definition: atlbase.h:39
#define FAILED(hr)
Definition: intsafe.h:51
#define TEXT(s)
Definition: k32.h:26
static PVOID ptr
Definition: dispmode.c:27
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
static LPUNKNOWN
Definition: ndr_ole.c:49
#define UNICODE_NULL
long LONG
Definition: pedump.c:60
#define REFIID
Definition: guiddef.h:118
#define REFCLSID
Definition: guiddef.h:117
#define DLLVER_PLATFORM_NT
Definition: shlwapi.h:1962
#define _countof(array)
Definition: sndvol32.h:68
#define StringCchCat
Definition: strsafe.h:317
ULONG dwPlatformId
Definition: rtltypes.h:241
ULONG dwOSVersionInfoSize
Definition: rtltypes.h:237
ULONG dwMajorVersion
Definition: rtltypes.h:238
ULONG dwMinorVersion
Definition: rtltypes.h:239
uint32_t * PULONG
Definition: typedefs.h:59
void * PVOID
Definition: typedefs.h:50
PVOID HANDLE
Definition: typedefs.h:73
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t * LPDWORD
Definition: typedefs.h:59
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
uint32_t * LPUINT
Definition: typedefs.h:59
WORD WORD PSZ PSZ pszFileName
Definition: vdmdbg.h:44
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
#define GetSystemDirectory
Definition: winbase.h:3842
#define GetModuleHandle
Definition: winbase.h:3827
#define GetVersionEx
Definition: winbase.h:3852
#define WINAPI
Definition: msvc.h:6
#define SM_DBCSENABLED
Definition: winuser.h:1005
int WINAPI GetSystemMetrics(_In_ int)
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define _tcslen
Definition: xmlstorage.h:198