[SHIMLIB] Update helper functions, add ShimLib_StringNDuplicateA
[reactos.git] / dll / appcompat / shims / shimlib / shimlib.h
1 /*
2 * PROJECT: ReactOS Shim helper library
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: ReactOS Shim Engine common functions / structures
5 * COPYRIGHT: Copyright 2016-2018 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8 #pragma once
9
10 typedef struct tagHOOKAPI
11 {
12 PCSTR LibraryName;
13 PCSTR FunctionName;
14 PVOID ReplacementFunction;
15 PVOID OriginalFunction;
16 PVOID Reserved[2];
17 } HOOKAPI, *PHOOKAPI;
18
19
20 PVOID ShimLib_ShimMalloc(SIZE_T dwSize);
21 VOID ShimLib_ShimFree(PVOID pData);
22 PCSTR ShimLib_StringDuplicateA(PCSTR szString);
23 PCSTR ShimLib_StringNDuplicateA(PCSTR szString, SIZE_T stringLength);
24 BOOL ShimLib_StrAEqualsW(PCSTR szString, PCWSTR wszString);
25
26
27 /* Forward events to generic handlers */
28 VOID ShimLib_Init(HINSTANCE hInstance);
29 VOID ShimLib_Deinit(VOID);
30 PHOOKAPI WINAPI ShimLib_GetHookAPIs(LPCSTR szCommandLine,LPCWSTR wszShimName,PDWORD pdwHookCount);
31 BOOL WINAPI ShimLib_NotifyShims(DWORD fdwReason, PVOID ptr);
32
33
34 /* Shims should respond to SHIM_REASON_XXXX in the Notify routines.
35 SHIM_NOTIFY_ codes are sent by apphelp, and translated to SHIM_REASON_ by the shimlib routines.
36 The only exception being SHIM_NOTIFY_ATTACH, that is also set for one-time init.
37 */
38
39 #define SHIM_REASON_INIT 100
40 #define SHIM_REASON_DEINIT 101
41 #define SHIM_REASON_DLL_LOAD 102 /* Arg: PLDR_DATA_TABLE_ENTRY */
42 #define SHIM_REASON_DLL_UNLOAD 103 /* Arg: PLDR_DATA_TABLE_ENTRY */
43
44 #define SHIM_NOTIFY_ATTACH 1
45 #define SHIM_NOTIFY_DETACH 2
46 #define SHIM_NOTIFY_DLL_LOAD 3 /* Arg: PLDR_DATA_TABLE_ENTRY */
47 #define SHIM_NOTIFY_DLL_UNLOAD 4 /* Arg: PLDR_DATA_TABLE_ENTRY */
48
49
50
51 typedef enum _SEI_LOG_LEVEL {
52 SEI_MSG = 1,
53 SEI_FAIL = 2,
54 SEI_WARN = 3,
55 SEI_INFO = 4,
56 } SEI_LOG_LEVEL;
57
58 BOOL WINAPIV SeiDbgPrint(SEI_LOG_LEVEL Level, PCSTR Function, PCSTR Format, ...);
59 extern ULONG g_ShimEngDebugLevel;
60
61 #if defined(IN_APPHELP)
62 /* Apphelp shimeng logging uses the function name */
63 #define SHIMENG_MSG(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_MSG, __FUNCTION__, fmt, ##__VA_ARGS__ ); } while (0)
64 #define SHIMENG_FAIL(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_FAIL, __FUNCTION__, fmt, ##__VA_ARGS__ ); } while (0)
65 #define SHIMENG_WARN(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_WARN, __FUNCTION__, fmt, ##__VA_ARGS__ ); } while (0)
66 #define SHIMENG_INFO(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_INFO, __FUNCTION__, fmt, ##__VA_ARGS__ ); } while (0)
67 #else
68 /* Shims use the shim name */
69 #define SHIM_MSG(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_MSG, SHIM_OBJ_NAME(g_szModuleName), fmt, ##__VA_ARGS__ ); } while (0)
70 #define SHIM_FAIL(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_FAIL, SHIM_OBJ_NAME(g_szModuleName), fmt, ##__VA_ARGS__ ); } while (0)
71 #define SHIM_WARN(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_WARN, SHIM_OBJ_NAME(g_szModuleName), fmt, ##__VA_ARGS__ ); } while (0)
72 #define SHIM_INFO(fmt, ...) do { if (g_ShimEngDebugLevel) SeiDbgPrint(SEI_INFO, SHIM_OBJ_NAME(g_szModuleName), fmt, ##__VA_ARGS__ ); } while (0)
73 #endif
74
75 typedef PHOOKAPI (WINAPI* _PVGetHookAPIs)(DWORD, PCSTR, PDWORD);
76 typedef BOOL (WINAPI* _PVNotify)(DWORD, PVOID);
77
78 typedef struct tagSHIMREG
79 {
80 _PVGetHookAPIs GetHookAPIs;
81 _PVNotify Notify;
82 PCSTR ShimName;
83 } SHIMREG, *PSHIMREG;
84
85
86 #if defined(_MSC_VER)
87 #define _SHMALLOC(x) __declspec(allocate(x))
88 #elif defined(__GNUC__)
89 #define _SHMALLOC(x) __attribute__ ((section (x) ))
90 #else
91 #error Your compiler is not supported.
92 #endif
93