Add an error message when LoadLibrary fails

develop
Clément Vuchener 2020-12-29 17:03:50 +01:00
parent eb6957a167
commit e7748e3f73
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);
}
}
}