我正在使用Windows API中的“LoadLibrary”,当我运行应用程序时,它会抛出错误代码126。我读到这可能是由依赖项引起的,我使用了一些应用程序(例如Dependency Walker)检查了一下,但一切都没问题。
应用程序中的LoadLibrary:
HMODULE dll_mod = LoadLibrary(L"path_to_dll");
if(dll_mod==NULL){
std::stringstream error;
error << "Could not load plugin located at:\n" << file_full.toStdString() << "\n" << "Error Code: " << GetLastError();
FreeLibrary(dll_mod);
return error.str();
}
插件代码:
#include "stdafx.h"
#define DLL_EXPORT
#define PLUGIN_STREAM __declspec(dllexport)
#include <iostream>
#include <vector>
using std::vector;
using std::string;
// Init event (After the loading)
extern "C"{
PLUGIN_STREAM int onInit(char* argv){
return 0;
}
PLUGIN_STREAM void pluginInfo(vector<string> & info){
info.push_back("media_event=false");
info.push_back("status_event=false");
info.push_back("send_event=true");
info.push_back("plugin_name='RadioStream'");
info.push_back("description='This plugin was designed for that people that wants to listen to radio music.\nYou can register your radio and play it later, also we have a gallery of radios that you can check.\nThis plugin is original of Volt and it's originally implemented in the application.'");
info.push_back("success:0");
info.push_back("error:1=Could not open data file");
info.push_back("error:2=Could not prepare plugin");
info.push_back("alert:40=Could not connect to that radio");
}
}
dll_mod==NULL
的情况下调用FreeLibrary(dll_mod)
不是一个好的做法。 - borisbnLoadLibrary
函数之后应该立即调用GetLastError
函数,因为stringstream
的构造函数和运算符<<可能会调用一些WinAPI,这可能会将最后的错误码清零。 - borisbn