xref: /reactos/sdk/lib/cicero/cicbase.h (revision 0c2cdcae)
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 #pragma once
9 
10 static inline LPVOID cicMemAlloc(SIZE_T size)
11 {
12     return LocalAlloc(0, size);
13 }
14 
15 static inline LPVOID cicMemAllocClear(SIZE_T size)
16 {
17     return LocalAlloc(LMEM_ZEROINIT, size);
18 }
19 
20 static inline LPVOID cicMemReAlloc(LPVOID ptr, SIZE_T newSize)
21 {
22     if (!ptr)
23         return LocalAlloc(LMEM_ZEROINIT, newSize);
24     return LocalReAlloc(ptr, newSize, LMEM_ZEROINIT);
25 }
26 
27 static inline void cicMemFree(LPVOID ptr)
28 {
29     if (ptr)
30         LocalFree(ptr);
31 }
32 
33 struct CicNoThrow { };
34 #define cicNoThrow CicNoThrow{}
35 
36 void* operator new(size_t size, const CicNoThrow&) noexcept;
37 void* operator new[](size_t size, const CicNoThrow&) noexcept;
38 void operator delete(void* ptr) noexcept;
39 void operator delete[](void* ptr) noexcept;
40 void operator delete(void* ptr, size_t size) noexcept;
41 void operator delete[](void* ptr, size_t size) noexcept;
42 
43 /* The flags of cicGetOSInfo() */
44 #define CIC_OSINFO_NT     0x01
45 #define CIC_OSINFO_2KPLUS 0x02
46 #define CIC_OSINFO_95     0x04
47 #define CIC_OSINFO_98PLUS 0x08
48 #define CIC_OSINFO_CJK    0x10
49 #define CIC_OSINFO_IMM    0x20
50 #define CIC_OSINFO_DBCS   0x40
51 #define CIC_OSINFO_XPPLUS 0x80
52 
53 EXTERN_C
54 void cicGetOSInfo(LPUINT puACP, LPDWORD pdwOSInfo);
55 
56 #ifdef __cplusplus
57 struct CicSystemModulePath
58 {
59     TCHAR m_szPath[MAX_PATH + 2];
60     SIZE_T m_cchPath;
61 
62     CicSystemModulePath()
63     {
64         m_szPath[0] = UNICODE_NULL;
65         m_cchPath = 0;
66     }
67 
68     BOOL Init(_In_ LPCTSTR pszFileName, _In_ BOOL bSysWinDir);
69 };
70 #endif
71 
72 // Get an instance handle that is already loaded
73 EXTERN_C
74 HINSTANCE
75 cicGetSystemModuleHandle(
76     _In_ LPCTSTR pszFileName,
77     _In_ BOOL bSysWinDir);
78 
79 // Load a system library
80 EXTERN_C
81 HINSTANCE
82 cicLoadSystemLibrary(
83     _In_ LPCTSTR pszFileName,
84     _In_ BOOL bSysWinDir);
85 
86 #ifdef __cplusplus
87 template <typename T_FN>
88 static inline BOOL
89 cicGetFN(HINSTANCE& hinstDLL, T_FN& fn, LPCTSTR pszDllName, LPCSTR pszFuncName)
90 {
91     if (fn)
92         return TRUE;
93     if (!hinstDLL)
94         hinstDLL = cicLoadSystemLibrary(pszDllName, FALSE);
95     if (!hinstDLL)
96         return FALSE;
97     fn = reinterpret_cast<T_FN>(GetProcAddress(hinstDLL, pszFuncName));
98     return !!fn;
99 }
100 #endif
101 
102 /* Is the current process on WoW64? */
103 EXTERN_C
104 BOOL cicIsWow64(VOID);
105 
106 EXTERN_C
107 HRESULT
108 cicRealCoCreateInstance(
109     _In_ REFCLSID rclsid,
110     _In_ LPUNKNOWN pUnkOuter,
111     _In_ DWORD dwClsContext,
112     _In_ REFIID iid,
113     _Out_ LPVOID *ppv);
114 
115 EXTERN_C
116 HRESULT
117 cicCoCreateInstance(
118     _In_ REFCLSID rclsid,
119     _In_ LPUNKNOWN pUnkOuter,
120     _In_ DWORD dwClsContext,
121     _In_ REFIID iid,
122     _Out_ LPVOID *ppv);
123 
124 // ole32!CoCreateInstance
125 typedef HRESULT (WINAPI *FN_CoCreateInstance)(
126     REFCLSID rclsid,
127     LPUNKNOWN pUnkOuter,
128     DWORD dwClsContext,
129     REFIID iid,
130     LPVOID *ppv);
131 
132 EXTERN_C BOOL TFInitLib(FN_CoCreateInstance fnCoCreateInstance = NULL);
133 EXTERN_C VOID TFUninitLib(VOID);
134