//#pragma comment(linker, "/subsystem:\"Windows\" /entry:\"mainCRTStartup\"") #include #include #include #include #include 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; }