From 0f7e664a4ab6d1a08bb4f6e977feaa4a2dda9c21 Mon Sep 17 00:00:00 2001 From: mrexodia Date: Tue, 14 Mar 2017 06:38:05 +0100 Subject: [PATCH] more versatile DLL loader extraction --- .gitignore | 11 +++---- TitanEngine/TitanEngine.Debugger.cpp | 43 ++++++++++++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 583176b..55ea33d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ x64/ build/ [Bb]in/ [Oo]bj/ +.vs/ # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets !packages/*/build/ @@ -153,8 +154,8 @@ $RECYCLE.BIN/ *.cscope_file_list *.bmarks *.depend -*.layout -*.orig -*.cbTemp -cov-* -coverity* +*.layout +*.orig +*.cbTemp +cov-* +coverity* diff --git a/TitanEngine/TitanEngine.Debugger.cpp b/TitanEngine/TitanEngine.Debugger.cpp index 2b9a422..4fd63cb 100644 --- a/TitanEngine/TitanEngine.Debugger.cpp +++ b/TitanEngine/TitanEngine.Debugger.cpp @@ -142,6 +142,29 @@ __declspec(dllexport) void* TITCALL InitDLLDebug(char* szFileName, bool ReserveM } } +static bool TryExtractDllLoader(bool failedBefore = false) +{ + wchar_t* szPath = wcsrchr(szDebuggerName, L'\\'); + if(szPath) + szPath[1] = '\0'; + wchar_t DLLLoaderName[64] = L""; +#ifdef _WIN64 + wsprintfW(DLLLoaderName, L"DLLLoader64_%.4X.exe", GetTickCount() & 0xFFFF); +#else + wsprintfW(DLLLoaderName, L"DLLLoader32_%.4X.exe", GetTickCount() & 0xFFFF); +#endif //_WIN64 + lstrcatW(szDebuggerName, DLLLoaderName); +#ifdef _WIN64 + if(EngineExtractResource("LOADERX64", szDebuggerName)) +#else + if(EngineExtractResource("LOADERX86", szDebuggerName)) +#endif //_WIN64 + return true; + return !failedBefore && + GetModuleFileNameW(engineHandle, szDebuggerName, _countof(szDebuggerName)) && + TryExtractDllLoader(true); +} + __declspec(dllexport) void* TITCALL InitDLLDebugW(wchar_t* szFileName, bool ReserveModuleBase, wchar_t* szCommandLine, wchar_t* szCurrentFolder, LPVOID EntryCallBack) { memset(szDebuggerName, 0, sizeof(szDebuggerName)); @@ -152,25 +175,7 @@ __declspec(dllexport) void* TITCALL InitDLLDebugW(wchar_t* szFileName, bool Rese szFileName = &szBackupDebuggedFileName[0]; } lstrcpyW(szDebuggerName, szFileName); - int i = lstrlenW(szDebuggerName); - while(szDebuggerName[i] != '\\' && i) - i--; - wchar_t DLLLoaderName[64] = L""; -#ifdef _WIN64 - wsprintfW(DLLLoaderName, L"DLLLoader64_%.4X.exe", GetTickCount() & 0xFFFF); -#else - wsprintfW(DLLLoaderName, L"DLLLoader32_%.4X.exe", GetTickCount() & 0xFFFF); -#endif - if(i) - lstrcpyW(szDebuggerName + i + 1, DLLLoaderName); - else - lstrcpyW(szDebuggerName, DLLLoaderName); - -#if defined(_WIN64) - if(EngineExtractResource("LOADERX64", szDebuggerName)) -#else - if(EngineExtractResource("LOADERX86", szDebuggerName)) -#endif + if(TryExtractDllLoader()) { DebugDebuggingDLL = true; int i = lstrlenW(szFileName);