我正在使用 Windows API 中的“LoadLibrary”,当我运行该应用程序时,它抛出一个错误代码 126。我读到它可能是由依赖项引起的,我检查了某些应用程序(如 Dependency Walker)有什么问题,但是一切都很好。
在应用程序中加载库:
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");
}
}
Windows dll 错误 126 可能有很多根本原因。 我发现最有用的调试方法是:
当您尝试加载一个 DLL 而这又需要另一个无法找到的 DLL 时,也会发生这种情况。
发生此错误的原因可能是 Windows/system32 文件夹中缺少 DLL 所依赖的某些 MFC 库(例如 mfc120.dll)。
就我而言,这完全是关于字符集与字符集。加载函数的形式。这是 Project/Properties/Configuration Properties/Advanced/Character Set 中的 visual studio 2019 设置,有两个选择:
1.Use Multi-Byte Character Set ->call it mb
2.Use Unicode Character Set -> call it uc
My test revealed:
const char* fileName = ".\\Debug\\Win32\\Dll1.dll";
void* module = LoadLibrary((LPCWSTR)fileName);
//compiles no mb, compiles uc, uc run fails with 126
void* module = LoadLibrary((LPCSTR)fileName);
//compiles mb,runs mb, no uc
void* module = LoadLibraryA(fileName); //note explicit A
//compiles mb,runs mb, compiles uc,runs uc
DWORD lasterror = GetLastError();//0 is ok
今天又把头撞到了126。 我了解到使 126 在前面的示例之上再次发生的一件事是从 my_dll 链接加载 java 的虚拟机 dll。在我的例子中,my_dll 需要将 jvm.dll 标记为“延迟加载”。 设置在项目级别: 配置属性/链接器/输入/延迟加载的 Dll 我写 jvm.dll 的地方; 这个错误是我可以重复的。
// PKG Fix - index.js
const isPkg = typeof process.pkg !== 'undefined';
const c_dir = isPkg ? path.dirname(process.execPath) : __dirname;
let soname;
if (os.platform() == 'win32') {
// Update path to load dependent dlls
let currentPath = process.env.Path;
let dllDirectory = path.resolve(path.join(c_dir, "win-x86_64"));
process.env.Path = dllDirectory + path.delimiter + currentPath;
soname = path.join(c_dir, "win-x86_64", "libvosk.dll");
} else if (os.platform() == 'darwin') {
soname = path.join(c_dir, "lib", "osx-universal", "libvosk.dylib");
} else {
soname = path.join(c_dir, "lib", "linux-x86_64", "libvosk.so");
}
就我而言,LoadLibrary(..) 中的 dll 名称不正确。