Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276/* * PROJECT: ReactOS Cicero * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later) * PURPOSE: Cicero base * COPYRIGHT: Copyright 2023-2024 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com> */
#include "precomp.h"#include "cicbase.h"#include <shlwapi.h>#include <stdlib.h>#include <string.h>#include <tchar.h>#include <strsafe.h>
void* operator new(size_t size, const CicNoThrow&) noexcept{ return cicMemAllocClear(size);}void* operator new[](size_t size, const CicNoThrow&) noexcept{ return cicMemAllocClear(size);}void operator delete(void* ptr) noexcept{ cicMemFree(ptr);}void operator delete[](void* ptr) noexcept{ cicMemFree(ptr);}void operator delete(void* ptr, size_t size) noexcept{ cicMemFree(ptr);}void operator delete[](void* ptr, size_t size) noexcept{ cicMemFree(ptr);}
LPVOID cicMemReCalloc(LPVOID mem, SIZE_T num, SIZE_T size) noexcept{ SIZE_T old_size, new_size = num * size; LPVOID ret;
if (!mem) return cicMemAllocClear(new_size);
old_size = LocalSize(mem); ret = cicMemReAlloc(mem, new_size); if (!ret) return NULL;
if (new_size > old_size) ZeroMemory((PBYTE)ret + old_size, new_size - old_size);
return ret;}
// FIXMEtypedef enum _PROCESSINFOCLASS{ ProcessBasicInformation = 0, ProcessDebugPort = 7, ProcessWow64Information = 26, ProcessImageFileName = 27, ProcessBreakOnTermination = 29} PROCESSINFOCLASS;
// FIXMEtypedef LONG NTSTATUS;
/* ntdll!NtQueryInformationProcess */typedef NTSTATUS (WINAPI *FN_NtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
EXTERN_CBOOL cicIsWow64(VOID){ static FN_NtQueryInformationProcess s_fnNtQueryInformationProcess = NULL; ULONG_PTR Value;
if (!s_fnNtQueryInformationProcess) { HMODULE hNTDLL = cicGetSystemModuleHandle(TEXT("ntdll.dll"), FALSE); if (!hNTDLL) return FALSE;
s_fnNtQueryInformationProcess = (FN_NtQueryInformationProcess)GetProcAddress(hNTDLL, "NtQueryInformationProcess"); if (!s_fnNtQueryInformationProcess) return FALSE; }
Value = 0; s_fnNtQueryInformationProcess(GetCurrentProcess(), ProcessWow64Information, &Value, sizeof(Value), NULL); return !!Value;}
EXTERN_Cvoid cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo){ *pdwOSInfo = 0;
/* Check OS version info */ OSVERSIONINFO VerInfo; VerInfo.dwOSVersionInfoSize = sizeof(VerInfo); GetVersionEx(&VerInfo); if (VerInfo.dwPlatformId == DLLVER_PLATFORM_NT) { *pdwOSInfo |= CIC_OSINFO_NT; if (VerInfo.dwMajorVersion >= 5) { *pdwOSInfo |= CIC_OSINFO_2KPLUS; if (VerInfo.dwMinorVersion > 0) *pdwOSInfo |= CIC_OSINFO_XPPLUS; } } else { if (VerInfo.dwMinorVersion >= 10) *pdwOSInfo |= CIC_OSINFO_98PLUS; else *pdwOSInfo |= CIC_OSINFO_95; }
/* Check codepage */ *puACP = GetACP(); switch (*puACP) { case 932: /* Japanese (Japan) */ case 936: /* Chinese (PRC, Singapore) */ case 949: /* Korean (Korea) */ case 950: /* Chinese (Taiwan, Hong Kong) */ *pdwOSInfo |= CIC_OSINFO_CJK; break; }
if (GetSystemMetrics(SM_IMMENABLED)) *pdwOSInfo |= CIC_OSINFO_IMM;
if (GetSystemMetrics(SM_DBCSENABLED)) *pdwOSInfo |= CIC_OSINFO_DBCS;}
// Get an instance handle that is already loadedEXTERN_CHINSTANCEcicGetSystemModuleHandle( _In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir){ CicSystemModulePath ModPath; if (!ModPath.Init(pszFileName, bSysWinDir)) return NULL; return GetModuleHandle(ModPath.m_szPath);}
// Load a system libraryEXTERN_CHINSTANCEcicLoadSystemLibrary( _In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir){ CicSystemModulePath ModPath; if (!ModPath.Init(pszFileName, bSysWinDir)) return NULL; return ::LoadLibrary(ModPath.m_szPath);}
BOOLCicSystemModulePath::Init( _In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir){ SIZE_T cchPath; if (bSysWinDir) { // Usually C:\Windows or C:\ReactOS cchPath = ::GetSystemWindowsDirectory(m_szPath, _countof(m_szPath)); } else { // Usually C:\Windows\system32 or C:\ReactOS\system32 cchPath = ::GetSystemDirectory(m_szPath, _countof(m_szPath)); }
m_szPath[_countof(m_szPath) - 1] = TEXT('\0'); // Avoid buffer overrun
if ((cchPath == 0) || (cchPath > _countof(m_szPath) - 2)) goto Failure;
// Add backslash if necessary if ((cchPath > 0) && (m_szPath[cchPath - 1] != TEXT('\\'))) { m_szPath[cchPath + 0] = TEXT('\\'); m_szPath[cchPath + 1] = TEXT('\0'); }
// Append pszFileName if (FAILED(StringCchCat(m_szPath, _countof(m_szPath), pszFileName))) goto Failure;
m_cchPath = _tcslen(m_szPath); return TRUE;
Failure: m_szPath[0] = UNICODE_NULL; m_cchPath = 0; return FALSE;}
static FN_CoCreateInstance_cicGetSetUserCoCreateInstance(FN_CoCreateInstance fnUserCoCreateInstance){ static FN_CoCreateInstance s_fn = NULL; if (fnUserCoCreateInstance) s_fn = fnUserCoCreateInstance; return s_fn;}
EXTERN_CHRESULTcicRealCoCreateInstance( _In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv){ static HINSTANCE s_hOle32 = NULL; static FN_CoCreateInstance s_fnCoCreateInstance = NULL; if (!s_fnCoCreateInstance) { if (!s_hOle32) s_hOle32 = cicLoadSystemLibrary(TEXT("ole32.dll"), FALSE); s_fnCoCreateInstance = (FN_CoCreateInstance)GetProcAddress(s_hOle32, "CoCreateInstance"); if (!s_fnCoCreateInstance) return E_NOTIMPL; }
return s_fnCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);}
/** * @implemented */HRESULTcicCoCreateInstance( _In_ REFCLSID rclsid, _In_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _In_ REFIID iid, _Out_ LPVOID *ppv){ // NOTE: It looks like Cicero wants to hook CoCreateInstance FN_CoCreateInstance fnUserCoCreateInstance = _cicGetSetUserCoCreateInstance(NULL); if (fnUserCoCreateInstance) return fnUserCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);
return cicRealCoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, ppv);}
/** * @implemented */EXTERN_CBOOLTFInitLib(FN_CoCreateInstance fnCoCreateInstance){ if (fnCoCreateInstance) _cicGetSetUserCoCreateInstance(fnCoCreateInstance); return TRUE;}