From ccbc64fe173cd28498ce04b5f11690a7bbc754f6 Mon Sep 17 00:00:00 2001 From: UE4SS Date: Fri, 23 Sep 2022 06:52:14 +0200 Subject: [PATCH] Output a more descriptive error message if LoadLibrary fails. --- src/main.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 7a60ce7..f53b620 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,37 @@ bool DumpTypesForDebugFile(const std::filesystem::path& PDBFilePath, const std:: return true; } +std::wstring GetLastErrorString(LPCTSTR lpszFunction) +{ + // Retrieve the system error message for the last-error code + + LPVOID lpMsgBuf; + LPVOID lpDisplayBuf; + DWORD dw = GetLastError(); + + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&lpMsgBuf, + 0, NULL); + + lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, + (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); + StringCchPrintf((LPTSTR)lpDisplayBuf, + LocalSize(lpDisplayBuf) / sizeof(TCHAR), + TEXT("%s failed with error %d: %s"), + lpszFunction, dw, lpMsgBuf); + auto ErrorAsString = std::wstring{(LPCTSTR)lpDisplayBuf}; + + LocalFree(lpMsgBuf); + LocalFree(lpDisplayBuf); + return ErrorAsString; +} + int main(int argc, const char** argv) { std::wcout << TEXT("Starting the UVTD") << std::endl; std::filesystem::path CurrentDirectory = std::filesystem::absolute(TEXT(".")); @@ -129,6 +161,7 @@ int main(int argc, const char** argv) { HMODULE DiaDllHandle = LoadLibraryW(DiaDllPath.wstring().c_str()); if (DiaDllHandle == nullptr) { std::wcout << TEXT("Failed to load msdia140.dll file, make sure it's in the run director at ") << DiaDllPath << std::endl; + std::wcout << GetLastErrorString(TEXT("LoadLibrary")) << std::endl; return 1; }