https://git.reactos.org/?p=reactos.git;a=commitdiff;h=8b15a5ecd703433de03e58...
commit 8b15a5ecd703433de03e58f6354b18f490a7e0bc Author: Mark Jansen mark.jansen@reactos.org AuthorDate: Sat Feb 24 14:57:56 2018 +0100 Commit: Mark Jansen mark.jansen@reactos.org CommitDate: Sun Apr 22 18:52:00 2018 +0200
[SDK][ACGENRAL] Add the shim IgnoreFreeLibrary --- dll/appcompat/shims/genral/CMakeLists.txt | 1 + dll/appcompat/shims/genral/ignorefreelib.c | 132 +++++++++++++++++++++++++++++ media/sdb/sysmain.xml | 3 + 3 files changed, 136 insertions(+)
diff --git a/dll/appcompat/shims/genral/CMakeLists.txt b/dll/appcompat/shims/genral/CMakeLists.txt index 725a2d85f6..86db9bbc91 100644 --- a/dll/appcompat/shims/genral/CMakeLists.txt +++ b/dll/appcompat/shims/genral/CMakeLists.txt @@ -5,6 +5,7 @@ spec2def(acgenral.dll genral.spec)
list(APPEND SOURCE ignoredbgout.c + ignorefreelib.c main.c themes.c genral.spec) diff --git a/dll/appcompat/shims/genral/ignorefreelib.c b/dll/appcompat/shims/genral/ignorefreelib.c new file mode 100644 index 0000000000..b0c7ebb991 --- /dev/null +++ b/dll/appcompat/shims/genral/ignorefreelib.c @@ -0,0 +1,132 @@ +/* + * PROJECT: ReactOS 'General' Shim library + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) + * PURPOSE: Ignore FreeLibrary calls + * COPYRIGHT: Copyright 2018 Mark Jansen (mark.jansen@reactos.org) + */ + +#define WIN32_NO_STATUS +#include <windef.h> +#include <winbase.h> +#include <shimlib.h> +#include <strsafe.h> + + +#define SHIM_NS IgnoreFreeLibrary +#include <setup_shim.inl> + +typedef BOOL(WINAPI* FREELIBRARYPROC)(HMODULE); + +const char** g_Names; +static int g_NameCount; + +BOOL WINAPI SHIM_OBJ_NAME(FreeLibrary)(HMODULE hModule) +{ + char Buffer[MAX_PATH], *Ptr = Buffer; + DWORD len, wanted = ARRAYSIZE(Buffer); + for (;;) + { + len = GetModuleFileNameA(hModule, Ptr, wanted); + if (len < wanted) + break; + + wanted *= 2; + if (Ptr != Buffer) + ShimLib_ShimFree(Ptr); + + Ptr = ShimLib_ShimMalloc(wanted); + if (!Ptr) + break; + } + + if (Ptr && len) + { + char* ModuleName = NULL; + int n; + for (; len; len--) + { + ModuleName = Ptr + len; + if (ModuleName[-1] == '/' || ModuleName[-1] == '\') + break; + } + for (n = 0; n < g_NameCount; ++n) + { + if (!stricmp(g_Names[n], ModuleName)) + { + SHIM_INFO("Prevented unload of %s\n", ModuleName); + if (Ptr && Ptr != Buffer) + ShimLib_ShimFree(Ptr); + return TRUE; + } + } + } + + if (Ptr && Ptr != Buffer) + ShimLib_ShimFree(Ptr); + + return CALL_SHIM(0, FREELIBRARYPROC)(hModule); +} + +static VOID InitIgnoreFreeLibrary(PCSTR CommandLine) +{ + PCSTR prev, cur; + int count = 1, n = 0; + const char** names; + + if (!CommandLine || !*CommandLine) + return; + + prev = CommandLine; + while ((cur = strchr(prev, ';'))) + { + count++; + prev = cur + 1; + } + + names = ShimLib_ShimMalloc(sizeof(char*) * count); + if (!names) + { + SHIM_WARN("Unable to allocate %u bytes\n", sizeof(char*) * count); + return; + } + + prev = CommandLine; + while ((cur = strchr(prev, ';'))) + { + names[n] = ShimLib_StringNDuplicateA(prev, cur - prev + 1); + if (!names[n]) + { + SHIM_WARN("Unable to allocate %u bytes\n", cur - prev + 2); + return; + } + n++; + prev = cur + 1; + } + names[n] = ShimLib_StringDuplicateA(prev); + if (!names[n]) + { + SHIM_WARN("Unable to allocate last string\n"); + return; + } + + g_Names = names; + g_NameCount = count; +} + +BOOL WINAPI SHIM_OBJ_NAME(Notify)(DWORD fdwReason, PVOID ptr) +{ + if (fdwReason == SHIM_NOTIFY_ATTACH) + { + SHIM_MSG("IgnoreFreeLibrary(%s)\n", SHIM_OBJ_NAME(g_szCommandLine)); + InitIgnoreFreeLibrary(SHIM_OBJ_NAME(g_szCommandLine)); + } + return TRUE; +} + + +#define SHIM_NOTIFY_FN SHIM_OBJ_NAME(Notify) +#define SHIM_NUM_HOOKS 1 +#define SHIM_SETUP_HOOKS \ + SHIM_HOOK(0, "KERNEL32.DLL", "FreeLibrary", SHIM_OBJ_NAME(FreeLibrary)) + +#include <implement_shim.inl> diff --git a/media/sdb/sysmain.xml b/media/sdb/sysmain.xml index c451c28d50..03b9b16982 100644 --- a/media/sdb/sysmain.xml +++ b/media/sdb/sysmain.xml @@ -206,6 +206,9 @@ <SHIM NAME="DisableThemes"> <DLLFILE>acgenral.dll</DLLFILE> </SHIM> + <SHIM NAME="IgnoreFreeLibrary"> + <DLLFILE>acgenral.dll</DLLFILE> + </SHIM> <SHIM NAME="VMHorizonSetup"> <DLLFILE>aclayers.dll</DLLFILE> </SHIM>