Merge remote-tracking branch 'cvuchener/fix-loadlibrary-errormessage' into develop

develop
lethosor 2021-01-06 21:00:03 -05:00
commit 462208cad3
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
1 changed files with 19 additions and 2 deletions

@ -24,6 +24,7 @@ distribution.
#define DFhackCExport extern "C" __declspec(dllexport)
#define NOMINMAX
#include <windows.h>
#include <stdint.h>
#include <vector>
@ -35,6 +36,7 @@ distribution.
#include "tinythread.h"
#include "modules/Graphic.h"
#include "../plugins/uicommon.h"
/*
* Plugin loading functions
@ -44,7 +46,22 @@ namespace DFHack
DFLibrary* GLOBAL_NAMES = (DFLibrary*)GetModuleHandle(nullptr);
DFLibrary * OpenPlugin (const char * filename)
{
return (DFLibrary *) LoadLibrary(filename);
DFLibrary *ret = (DFLibrary *) LoadLibrary(filename);
if (!ret) {
DWORD error = GetLastError();
LPSTR buf = NULL;
DWORD len = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
if (len > 0) {
std::string message = buf;
std::cerr << "LoadLibrary " << filename << ": " << rtrim(message) << " (" << error << ")" << std::endl;
LocalFree(buf);
}
else {
std::cerr << "LoadLibrary " << filename << ": unknown error " << error << std::endl;
}
}
return ret;
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
@ -54,4 +71,4 @@ namespace DFHack
{
FreeLibrary((HMODULE) plugin);
}
}
}