From 95ad1870152b498b162bd9207989f40fe227a7e0 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Fri, 17 Jan 2025 21:40:59 +0800 Subject: [PATCH 1/2] add pin to avoid unload --- lib/YaeAchievementLib.vcxproj | 1 + lib/ntprivate.h | 9 +++++++++ lib/src/dllmain.cpp | 6 ++++++ lib/src/ntprivate.h | 9 +++++++++ 4 files changed, 25 insertions(+) create mode 100644 lib/ntprivate.h create mode 100644 lib/src/ntprivate.h diff --git a/lib/YaeAchievementLib.vcxproj b/lib/YaeAchievementLib.vcxproj index 19d11eb..63d362c 100644 --- a/lib/YaeAchievementLib.vcxproj +++ b/lib/YaeAchievementLib.vcxproj @@ -96,6 +96,7 @@ + diff --git a/lib/ntprivate.h b/lib/ntprivate.h new file mode 100644 index 0000000..be91af9 --- /dev/null +++ b/lib/ntprivate.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +#pragma comment(lib, "ntdll.lib") + +#define LDR_ADDREF_DLL_PIN 0x00000001 + +EXTERN_C NTSYSAPI NTSTATUS NTAPI LdrAddRefDll(_In_ ULONG Flags, _In_ PVOID DllHandle); \ No newline at end of file diff --git a/lib/src/dllmain.cpp b/lib/src/dllmain.cpp index 69e5d0e..e141d80 100644 --- a/lib/src/dllmain.cpp +++ b/lib/src/dllmain.cpp @@ -9,6 +9,7 @@ #include "util.h" #include "il2cpp-init.h" #include "il2cpp-types.h" +#include "ntprivate.h" CRITICAL_SECTION CriticalSection; void SetBreakpoint(HANDLE thread, uintptr_t address, bool enable, uint8_t index = 0); @@ -200,6 +201,11 @@ BOOL __stdcall DllMain(HMODULE hInstance, DWORD fdwReason, LPVOID lpReserved) if (fdwReason == DLL_PROCESS_ATTACH) { + if (hInstance) + { + LdrAddRefDll(LDR_ADDREF_DLL_PIN, hInstance); + } + if (const auto hThread = CreateThread(nullptr, 0, ThreadProc, hInstance, 0, nullptr)) { CloseHandle(hThread); } diff --git a/lib/src/ntprivate.h b/lib/src/ntprivate.h new file mode 100644 index 0000000..be91af9 --- /dev/null +++ b/lib/src/ntprivate.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +#pragma comment(lib, "ntdll.lib") + +#define LDR_ADDREF_DLL_PIN 0x00000001 + +EXTERN_C NTSYSAPI NTSTATUS NTAPI LdrAddRefDll(_In_ ULONG Flags, _In_ PVOID DllHandle); \ No newline at end of file From d1bd0c7d7b72aeb85f822fe4ea8b1a79ed4102d5 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Fri, 17 Jan 2025 21:55:50 +0800 Subject: [PATCH 2/2] fix --- lib/YaeAchievementLib.vcxproj | 2 +- lib/ntprivate.h | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 lib/ntprivate.h diff --git a/lib/YaeAchievementLib.vcxproj b/lib/YaeAchievementLib.vcxproj index 63d362c..06aad96 100644 --- a/lib/YaeAchievementLib.vcxproj +++ b/lib/YaeAchievementLib.vcxproj @@ -96,11 +96,11 @@ - + diff --git a/lib/ntprivate.h b/lib/ntprivate.h deleted file mode 100644 index be91af9..0000000 --- a/lib/ntprivate.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include - -#pragma comment(lib, "ntdll.lib") - -#define LDR_ADDREF_DLL_PIN 0x00000001 - -EXTERN_C NTSYSAPI NTSTATUS NTAPI LdrAddRefDll(_In_ ULONG Flags, _In_ PVOID DllHandle); \ No newline at end of file