Skip to content

Commit

Permalink
Automatically load up reshade
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandromrc authored Sep 4, 2023
1 parent bf526a7 commit 658c8d4
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions LibraryHijacker/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#include <CString>
#include <atlstr.h>
#include "../MinHook/include/MinHook.h"
#include "pattern_sc.hpp"

bool hijacked = false;
bool hijacked_d3d11 = false;
bool hijacked_dxgi = false;

typedef HMODULE(WINAPI* TYPE_LoadLibraryA)(LPCSTR);
TYPE_LoadLibraryA g_loadLibraryA_original = NULL;
HMODULE WINAPI LoadLibraryA_replacement(_In_ LPCSTR lpFileName)
{
if (hijacked == false) // avoid doing it again as our wrapper module might actually load real d3d11.dll!
if (hijacked_d3d11== false) // avoid doing it again as our wrapper module might actually load real d3d11.dll!
{
if (strstr(lpFileName, "D3D11") != nullptr || strstr(lpFileName, "d3d11"))
{
Expand All @@ -28,14 +30,41 @@ HMODULE WINAPI LoadLibraryA_replacement(_In_ LPCSTR lpFileName)
*lastSlash = '\0';
strcat_s(dllPath, "\\d3d11.dll");
lpFileName = dllPath;
hijacked = true;
hijacked_d3d11 = true;
}
}
}
}

if (hijacked_dxgi == false) // avoid doing it again as our wrapper module might actually load real d3d11.dll!
{
if (strstr(lpFileName, "dxgi") != nullptr || strstr(lpFileName, "DXGI"))
{
char path[MAX_PATH];
if (GetModuleFileNameA(NULL, path, MAX_PATH) != 0)
{
char dllPath[MAX_PATH];
strcpy_s(dllPath, path);
char* lastSlash = strrchr(dllPath, '\\');
if (lastSlash != NULL)
{
*lastSlash = '\0';
strcat_s(dllPath, "\\dxgi.dll");
if (PathFileExistsA(dllPath)) // check if dxgi.dll exists
{
lpFileName = dllPath;
hijacked_dxgi = true;
}
}
}
}
}

return g_loadLibraryA_original(lpFileName);
}

using namespace memory;

bool installHooks()
{
// Initialize MinHook.
Expand All @@ -59,34 +88,6 @@ bool installHooks()
return true;
}

const char* mutexDetect = "hijackmutex1337";
HANDLE hMutex;

bool IsAlreadyInjected()
{
hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexDetect);
if (hMutex)
{
CloseHandle(hMutex);
return true;
}

// The mutex does not exist, indicating the DLL is not injected yet
// Create the mutex to prevent further injections
HANDLE hNewMutex = CreateMutexA(NULL, TRUE, mutexDetect);
if (!hNewMutex)
{
// Failed to create mutex
// Handle the error appropriately
}

if (hNewMutex != 0)
CloseHandle(hNewMutex);

return false;
}


BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
Expand All @@ -96,32 +97,23 @@ BOOL APIENTRY DllMain(HMODULE hModule,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:

if (IsAlreadyInjected())
{
MessageBoxA(NULL, "Do you want to crash your game? Seems like you like desktop!", "Injection Error", MB_ICONERROR);
return FALSE;
}
installHooks();
std::thread([]() {
if (hijacked)
if (hijacked_d3d11 || hijacked_dxgi)
{
if (MH_DisableHook(MH_ALL_HOOKS) == MH_OK)
{
MH_Uninitialize();
HMODULE hModule = GetModuleHandle(NULL);
ReleaseMutex(hMutex);
FreeLibraryAndExitThread(hModule, 0);
}
}
}).detach();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
ReleaseMutex(hMutex);
case DLL_PROCESS_DETACH:
ReleaseMutex(hMutex);
break;
}
return TRUE;
}
}

0 comments on commit 658c8d4

Please sign in to comment.