mirror of
https://github.com/kurikomoe/L4D2Fix.git
synced 2026-10-10 04:43:07 +08:00
81d7faa0da
switch to exe loader method to make sure the dll is properly injected
72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
//#pragma comment(linker, "/subsystem:\"Windows\" /entry:\"mainCRTStartup\"")
|
|
#include <windows.h>
|
|
#include <string>
|
|
#include <filesystem>
|
|
#include <shellapi.h>
|
|
|
|
#include <detours/detours.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
const wchar_t* game_name = L"left4dead2.exe";
|
|
|
|
int WINAPI wWinMain(
|
|
_In_ HINSTANCE hInstance,
|
|
_In_opt_ HINSTANCE hPrevInstance,
|
|
_In_ LPWSTR lpwCmdLine,
|
|
_In_ int nShowCmd
|
|
) {
|
|
WCHAR working_path[MAX_PATH];
|
|
GetModuleFileNameW(nullptr, working_path, MAX_PATH);
|
|
|
|
// Change the working directory to the directory containing the DLL.
|
|
fs::path path(working_path);
|
|
SetCurrentDirectoryW(path.parent_path().wstring().c_str());
|
|
|
|
LPCSTR dll_path = "kpatch.dll";
|
|
|
|
LPCWSTR target_exe_path = game_name;
|
|
|
|
// if (std::filesystem::exists(L"left4dead2.exe.orig")) {
|
|
// target_exe_path = L"left4dead2.exe.orig";
|
|
// } else {
|
|
// target_exe_path = game_name;
|
|
// }
|
|
|
|
STARTUPINFOW si;
|
|
PROCESS_INFORMATION pi;
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED;
|
|
|
|
SetLastError(0);
|
|
if (TRUE != DetourCreateProcessWithDllExW(
|
|
target_exe_path,
|
|
lpwCmdLine,
|
|
nullptr,
|
|
nullptr,
|
|
TRUE,
|
|
dwFlags,
|
|
nullptr,
|
|
nullptr,
|
|
&si,
|
|
&pi,
|
|
dll_path,
|
|
nullptr)) {
|
|
auto dwError = GetLastError();
|
|
printf("DetourCreateProcessWithDllEx failed with error %ld\n", dwError);
|
|
|
|
ExitProcess(9009);
|
|
}
|
|
|
|
ResumeThread(pi.hThread);
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
return 0;
|
|
}
|