ReactOS 0.4.16-dev-1946-g52006dd
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
42{
44 LPVOID ret;
45
46 if (!mem)
48
51 if (!ret)
52 return NULL;
53
54 if (new_size > old_size)
56
57 return ret;
58}
59
60// FIXME
62{
69
70// FIXME
71typedef LONG NTSTATUS;
72
73/* ntdll!NtQueryInformationProcess */
75
78{
79 static FN_NtQueryInformationProcess s_fnNtQueryInformationProcess = NULL;
81
82 if (!s_fnNtQueryInformationProcess)
83 {
84 HMODULE hNTDLL = cicGetSystemModuleHandle(TEXT("ntdll.dll"), FALSE);
85 if (!hNTDLL)
86 return FALSE;
87
88 s_fnNtQueryInformationProcess =
89 (FN_NtQueryInformationProcess)GetProcAddress(hNTDLL, "NtQueryInformationProcess");
90 if (!s_fnNtQueryInformationProcess)
91 return FALSE;
92 }
93
94 Value = 0;
95 s_fnNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information,
96 &Value, sizeof(Value), NULL);
97 return !!Value;
98}
99
101void cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo)
102{
103 *pdwOSInfo = 0;
104
105 /* Check OS version info */
106 OSVERSIONINFO VerInfo;
107 VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
108 GetVersionEx(&VerInfo);
109 if (VerInfo.dwPlatformId == DLLVER_PLATFORM_NT)
110 {
111 *pdwOSInfo |= CIC_OSINFO_NT;
112 if (VerInfo.dwMajorVersion >= 5)
113 {
114 *pdwOSInfo |= CIC_OSINFO_2KPLUS;
115 if (VerInfo.dwMinorVersion > 0)
116 *pdwOSInfo |= CIC_OSINFO_XPPLUS;
117 }
118 }
119 else
120 {
121 if (VerInfo.dwMinorVersion >= 10)
122 *pdwOSInfo |= CIC_OSINFO_98PLUS;
123 else
124 *pdwOSInfo |= CIC_OSINFO_95;
125 }
126
127 /* Check codepage */
128 *puACP = GetACP();
129 switch (*puACP)
130 {
131 case 932: /* Japanese (Japan) */
132 case 936: /* Chinese (PRC, Singapore) */
133 case 949: /* Korean (Korea) */
134 case 950: /* Chinese (Taiwan, Hong Kong) */
135 *pdwOSInfo |= CIC_OSINFO_CJK;
136 break;
137 }
138
139 if (GetSystemMetrics(SM_IMMENABLED))
140 *pdwOSInfo |= CIC_OSINFO_IMM;
141
143 *pdwOSInfo |= CIC_OSINFO_DBCS;
144}
145
146// Get an instance handle that is already loaded
151 _In_ BOOL bSysWinDir)
152{
153 CicSystemModulePath ModPath;
154 if (!ModPath.Init(pszFileName, bSysWinDir))
155 return NULL;
156 return GetModuleHandle(ModPath.m_szPath);
157}
158
159// Load a system library
164 _In_ BOOL bSysWinDir)
165{
166 CicSystemModulePath ModPath;
167 if (!ModPath.Init(pszFileName, bSysWinDir))
168 return NULL;
169 return ::LoadLibrary(ModPath.m_szPath);
170}
171
172BOOL
173CicSystemModulePath::Init(
175 _In_ BOOL bSysWinDir)
176{
177 SIZE_T cchPath;
178 if (bSysWinDir)
179 {
180 // Usually C:\Windows or C:\ReactOS
181 cchPath = ::GetSystemWindowsDirectory(m_szPath, _countof(m_szPath));
182 }
183 else
184 {
185 // Usually C:\Windows\system32 or C:\ReactOS\system32
186 cchPath = ::GetSystemDirectory(m_szPath, _countof(m_szPath));
187 }
188
189 m_szPath[_countof(m_szPath) - 1] = TEXT('\0'); // Avoid buffer overrun
190
191 if ((cchPath == 0) || (cchPath > _countof(m_szPath) - 2))
192 goto Failure;
193
194 // Add backslash if necessary
195 if ((cchPath > 0) && (m_szPath[cchPath - 1] != TEXT('\\')))
196 {
197 m_szPath[cchPath + 0] = TEXT('\\');
198 m_szPath[cchPath + 1] = TEXT('\0');
199 }
200
201 // Append pszFileName
202 if (FAILED(StringCchCat(m_szPath, _countof(m_szPath), pszFileName)))
203 goto Failure;
204
205 m_cchPath = _tcslen(m_szPath);
206 return TRUE;
207
208Failure:
209 m_szPath[0] = UNICODE_NULL;
210 m_cchPath = 0;
211 return FALSE;
212}
213
216{
217 static FN_CoCreateInstance s_fn = NULL;
218 if (fnUserCoCreateInstance)
219 s_fn = fnUserCoCreateInstance;
220 return s_fn;
221}
222
226 _In_ REFCLSID rclsid,
227 _In_ LPUNKNOWN pUnkOuter,
228 _In_ DWORD dwClsContext,
229 _In_ REFIID iid,
231{
232 static HINSTANCE s_hOle32 = NULL;
233 static FN_CoCreateInstance s_fnCoCreateInstance = NULL;
234 if (!s_fnCoCreateInstance)
235 {
236 if (!s_hOle32)
237 s_hOle32 = cicLoadSystemLibrary(TEXT("ole32.dll"), FALSE);
238 s_fnCoCreateInstance = (FN_CoCreateInstance)GetProcAddress(s_hOle32, "CoCreateInstance");
239 if (!s_fnCoCreateInstance)
240 return E_NOTIMPL;
241 }
242
243 return s_fnCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
244}
245
251 _In_ REFCLSID rclsid,
252 _In_ LPUNKNOWN pUnkOuter,
253 _In_ DWORD dwClsContext,
254 _In_ REFIID iid,
256{
257 // NOTE: It looks like Cicero wants to hook CoCreateInstance
259 if (fnUserCoCreateInstance)
260 return fnUserCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
261
262 return cicRealCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
263}
264
269BOOL
271{
272 if (fnCoCreateInstance)
273 _cicGetSetUserCoCreateInstance(fnCoCreateInstance);
274 return TRUE;
275}
#define EXTERN_C
Definition: basetyps.h:12
LONG NTSTATUS
Definition: cicbase.cpp:71
EXTERN_C BOOL cicIsWow64(VOID)
Definition: cicbase.cpp:77
EXTERN_C HINSTANCE cicLoadSystemLibrary(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir)
Definition: cicbase.cpp:162
NTSTATUS(WINAPI * FN_NtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG)
Definition: cicbase.cpp:74
EXTERN_C HRESULT cicRealCoCreateInstance(_In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv)
Definition: cicbase.cpp:225
_PROCESSINFOCLASS
Definition: cicbase.cpp:62
@ ProcessDebugPort
Definition: cicbase.cpp:64
@ ProcessBreakOnTermination
Definition: cicbase.cpp:67
@ ProcessBasicInformation
Definition: cicbase.cpp:63
@ ProcessWow64Information
Definition: cicbase.cpp:65
@ ProcessImageFileName
Definition: cicbase.cpp:66
HRESULT cicCoCreateInstance(_In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv)
Definition: cicbase.cpp:250
LPVOID cicMemReCalloc(LPVOID mem, SIZE_T num, SIZE_T size) noexcept
Definition: cicbase.cpp:41
static FN_CoCreateInstance _cicGetSetUserCoCreateInstance(FN_CoCreateInstance fnUserCoCreateInstance)
Definition: cicbase.cpp:215
EXTERN_C HINSTANCE cicGetSystemModuleHandle(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir)
Definition: cicbase.cpp:149
EXTERN_C void cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo)
Definition: cicbase.cpp:101
EXTERN_C BOOL TFInitLib(FN_CoCreateInstance fnCoCreateInstance)
Definition: cicbase.cpp:270
enum _PROCESSINFOCLASS PROCESSINFOCLASS
Definition: loader.c:63
#define CIC_OSINFO_IMM
Definition: cicbase.h:61
static void cicMemFree(LPVOID ptr)
Definition: cicbase.h:20
#define CIC_OSINFO_NT
Definition: cicbase.h:56
#define CIC_OSINFO_DBCS
Definition: cicbase.h:62
HRESULT(WINAPI * FN_CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
Definition: cicbase.h:137
static LPVOID cicMemReAlloc(LPVOID ptr, SIZE_T newSize)
Definition: cicbase.h:26
#define CIC_OSINFO_2KPLUS
Definition: cicbase.h:57
#define CIC_OSINFO_XPPLUS
Definition: cicbase.h:63
#define CIC_OSINFO_95
Definition: cicbase.h:58
static LPVOID cicMemAllocClear(SIZE_T size)
Definition: cicbase.h:15
#define CIC_OSINFO_CJK
Definition: cicbase.h:60
#define CIC_OSINFO_98PLUS
Definition: cicbase.h:59
#define E_NOTIMPL
Definition: ddrawi.h:99
static LPVOID LPUNKNOWN
Definition: dinput.c:53
#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:2023
return ret
Definition: mutex.c:146
size_t const old_size
Definition: expand.cpp:65
size_t const new_size
Definition: expand.cpp:66
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizeiptr size
Definition: glext.h:5919
GLuint GLuint num
Definition: glext.h:9618
SIZE_T NTAPI LocalSize(HLOCAL hMem)
Definition: heapmem.c:1794
REFIID LPVOID * ppv
Definition: atlbase.h:39
#define FAILED(hr)
Definition: intsafe.h:51
#define TEXT(s)
Definition: k32.h:28
#define ZeroMemory
Definition: minwinbase.h:31
static PVOID ptr
Definition: dispmode.c:27
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define UNICODE_NULL
BYTE * PBYTE
Definition: pedump.c:66
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:117
#define _countof(array)
Definition: sndvol32.h:70
#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
Definition: mem.c:349
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:3591
#define GetModuleHandle
Definition: winbase.h:3576
#define GetVersionEx
Definition: winbase.h:3601
#define WINAPI
Definition: msvc.h:6
#define SM_DBCSENABLED
Definition: winuser.h:1016
int WINAPI GetSystemMetrics(_In_ int)
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define _tcslen
Definition: xmlstorage.h:198