32 #include <charon-core/WindowsPluginLoader.h>
38 const std::string & n,
39 std::vector<std::string> &plpaths,
47 LPCTSTR WindowsPluginLoader::lastError(LPTSTR func)
const {
52 DWORD dw = GetLastError();
54 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
55 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL,
56 SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
58 = (LPVOID) LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR) lpMsgBuf)
59 + lstrlen((LPCTSTR) func) + 40) *
sizeof(TCHAR));
61 StringCchPrintf((LPTSTR) lpDisplayBuf, LocalSize(lpDisplayBuf)
62 /
sizeof(TCHAR), TEXT(
""));
65 StringCchPrintf((LPTSTR) lpDisplayBuf, LocalSize(lpDisplayBuf)
66 /
sizeof(TCHAR), TEXT(
"%s failed with error %d: %s"), func, dw,
70 return (LPCTSTR) lpDisplayBuf;
75 std::string path, pathS, pathDir;
77 DWORD lastErrorCode = 0;
78 std::string lastErrorMsg;
81 for(std::vector<std::string>::const_iterator cur =
pluginPaths.begin();
86 pathS = *cur +
"\\" + pluginName +
libSuffix +
".dll";
88 path = *cur +
"\\lib" + pluginName +
".dll";
89 pathS = *cur +
"\\lib" + pluginName + libSuffix +
".dll";
91 if(libSuffix.size() > 0) {
103 throw PluginException(
"Failed to load the plugin \"" +
pluginName
105 +
".dll could not be found. (Possible suffix: "+
libSuffix +
")",
108 sout <<
"(DD) File: " << path << std::endl;
113 if (
_versionCheck != PluginManagerInterface::PluginVersionIgnore) {
116 std::string noVersionMsg =
"The plugin contains no valid charon-core version information." ;
119 DWORD size = GetFileVersionInfoSize(path.c_str(), &handle);
121 throw PluginException( noVersionMsg,
pluginName,
124 BYTE* versionInfo =
new BYTE[size];
125 if (!GetFileVersionInfo(path.c_str(), handle, size, versionInfo)) {
126 delete[] versionInfo;
127 throw PluginException( noVersionMsg,
pluginName,
131 struct LANGANDCODEPAGE {
139 BOOL res = VerQueryValue(versionInfo,
140 TEXT(
"\\VarFileInfo\\Translation"),
141 (LPVOID*)&lpTranslate,
143 if (res == 0 || dwBytes == 0) {
144 delete[] versionInfo;
145 throw PluginException( noVersionMsg,
pluginName,
151 for(
int i=0; i < (dwBytes/
sizeof(
struct LANGANDCODEPAGE)); i++ ) {
153 subBlock =
new char[50] ;
154 BOOL hr = StringCchPrintf(subBlock, 50,
155 TEXT(
"\\StringFileInfo\\%04x%04x\\ProductName"),
156 lpTranslate[i].wLanguage,
157 lpTranslate[i].wCodePage);
159 delete[] versionInfo;
161 throw PluginException( noVersionMsg,
pluginName,
167 BOOL res = VerQueryValue(versionInfo,
169 (LPVOID*)&infoBuffer,
172 if(res == 0 || bufferLen == 0 || std::string(
"Charon-Suite") != infoBuffer) {
173 delete[] versionInfo;
175 throw PluginException( noVersionMsg,
pluginName,
179 hr = StringCchPrintf(subBlock, 50,
180 TEXT(
"\\StringFileInfo\\%04x%04x\\ProductVersion"),
181 lpTranslate[i].wLanguage,
182 lpTranslate[i].wCodePage);
184 delete[] versionInfo;
186 throw PluginException( noVersionMsg,
pluginName,
190 res = VerQueryValue(versionInfo,
192 (LPVOID*)&infoBuffer,
194 if (res && bufferLen) {
199 std::string version(infoBuffer) ;
200 if(charonCoreVersion != version) {
201 delete[] versionInfo;
203 throw PluginException(
"Plugin \"" +
pluginName
204 +
"\" was linked against charon-core version " + version
205 +
"\n\tbut we are using charon-core version "
213 delete[] versionInfo;
215 catch(PluginException& excpt)
217 if(
_versionCheck < PluginManagerInterface::PluginVersionDiscard)
219 std::regex e (
"\\(EE\\)");
220 std::string msg = excpt.what() ;
221 sout <<
"(WW) " << std::regex_replace (msg,e,std::string(
"(WW)")) << std::endl ;
232 lastErrorCode = GetLastError() ;
233 lastErrorMsg = lastError(
"LoadLibrary") ;
242 if (lastErrorMsg.empty()) {
243 addInfo +=
"This is usually caused by missing dll dependencies.";
246 addInfo += lastErrorMsg ;
247 if(lastErrorCode == ERROR_MOD_NOT_FOUND) {
248 addInfo +=
"This is usually caused by missing dll dependencies." ;
251 throw PluginException(
256 throw PluginException(
"Failed to load the plugin \"" +
pluginName
257 +
"\". Maybe the file is damaged.",
pluginName,
265 lastErrorCode = GetLastError() ;
266 lastErrorMsg = lastError(
"GetProcAddress") ;
271 std::string errorMsg;
272 if (lastErrorCode == ERROR_PROC_NOT_FOUND) {
273 errorMsg +=
"function \"create\" missing in dll file";
276 errorMsg +=
"Invalid plugin format.\nDescription of the error:\n";
277 errorMsg += lastErrorMsg;
279 throw PluginException(
283 throw PluginException(
284 "function \"create\" missing in dll file",
293 lastErrorCode = GetLastError() ;
294 lastErrorMsg = lastError(
"GetProcAddress") ;
299 std::string errorMsg;
300 if (lastErrorCode == ERROR_PROC_NOT_FOUND) {
301 errorMsg +=
"function \"create\" missing in dll file";
304 errorMsg +=
"Invalid plugin format.\nDescription of the error:\n";
305 errorMsg += lastErrorMsg;
307 throw PluginException(
311 throw PluginException(
312 "function \"destroy\" missing in dll file",
321 <<
"(WW) \tfunction \"getBuildType\" missing in dll file\n"
322 <<
"(WW) \tno checks for runtime compatibilty are perfomed"
332 throw PluginException(
333 "This Plugin is build in DEBUG "
334 "configuration while charon-core is in RELEASE Mode.\n"
335 "Plugin will not be used as runtime libraries are incompatible",
344 throw PluginException(
345 "This Plugin is build in RELEASE "
346 "configuration while charon-core is in DEBUG Mode.\n"
347 "Plugin will not be used as runtime libraries are incompatible",
359 sout <<
"(DD) Successfully unloaded plugin \"" <<
pluginName <<
"\"."
362 throw PluginException(
"Plugin \"" +
pluginName +
"\" is not loaded.",
File is a shared object but misses required functions.
charon-core Version Information
#define CHARON_CORE_VERSION
charon-core version string
virtual void unload()
Unloads the plugin.
there is a missmatch between the current charon-core version and the version the plugin was compiled ...
build_type
defined build type
SplitStream sout
Dummy instance for usage in other files (for interface too).
template_type
Integer which represents a template type.
File is damaged, unreadable, not a shared object or the user access rights are insufficient.
Base class for serializable objects.
Declaration of class ParameterFile.
void(* destroy)(ParameteredObject *)
Function pointer to the destructor of the plugin.
the object was compiled in release mode
ParameteredObject *(* create)(const std::string &, ParameteredObject::template_type)
Function pointer to the constructor of the plugin.
std::vector< std::string > & pluginPaths
Paths where the plugins are stored.
Plugin was linked against incompatible runtime library.
std::string pluginName
The name of the Plugin (without prefix "lib" and without extension)
Plugin file doesn't exist or is placed in the wrong directory.
std::string & libSuffix
Lib suffix e.g. _d for debug builds.
PluginVersionCheckLevel
info how to handle version information
virtual void load()
Loads the plugin.
the object was compiled in debug mode
While unloading a plugin or deleting an instance: The plugin is not loaded.
PluginManagerInterface::PluginVersionCheckLevel _versionCheck
should the Loader ignore embedded charon-core version strings?
WindowsPluginLoader(const std::string &n, std::vector< std::string > &plpaths, std::string &lSuffix, PluginManagerInterface::PluginVersionCheckLevel versionCheck=PluginManagerInterface::PluginVersionIgnore)
Default constructor.
HINSTANCE hInstLibrary
Points to the loaded library.
virtual ~WindowsPluginLoader()
Default destructor.
Version information for the plugin is missing while explicit version check was requested.
ParameteredObject::build_type(* getBuildType)()
Function pointer to the getBuildType function of the plugin.
Abstract base class for a plugin loader.