DLL Load Library - 错误代码126

55

我正在使用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");
}
}

你在哪个平台上进行编程?我刚在谷歌上输入了“LoadLibrary failed”,它立即自动补全为“LoadLibrary failed with error code 126”,给出了大约41,000个结果,其中包括如何解决此问题的YouTube视频。这些链接中真的没有一个有用吗? - Andy Prowl
我跟随了一些教程,他们谈到了依赖项...关于dll不存在的问题,好吧,我已经搜索了4个小时,无法通过任何教程修复它,我已经检查了依赖项.... :S - Spamdark
4
dll_mod==NULL的情况下调用FreeLibrary(dll_mod)不是一个好的做法。 - borisbn
3
你确定你没有试图从32位可执行文件中加载64位库?或反之亦然?或者你的32位DLL直接或间接依赖于64位库?还是反过来?你已经检查过你的DLL文件对你的应用程序是可见的吗?DLL文件在哪里?EXE文件在哪里? - Andy Prowl
3
另外需要注意的是,在调用LoadLibrary函数之后应该立即调用GetLastError函数,因为stringstream的构造函数和运算符<<可能会调用一些WinAPI,这可能会将最后的错误码清零。 - borisbn
8个回答

107
Windows dll错误126可能有许多根本原因。我发现最有用的调试方法是:
  1. 使用依赖项查找器查找任何明显的问题(您已经完成了此步骤)
  2. 使用来自Microsoft的Sysinternals实用程序Process Monitor https://learn.microsoft.com/en-us/sysinternals/downloads/procmon跟踪所有文件访问,同时您的dll正在尝试加载。使用此实用程序,您将看到该dll正在尝试获取的所有内容,并且通常可以从中确定问题。

4
明白了!有了那个工具,我找到了错误,非常感谢! - Spamdark
18
不幸的是,Dependency Walker 经常无法显示缺失的依赖项,并且经常显示错误的结果。几年前它似乎更加可靠--可能是因为没有跟上最近操作系统的步伐。 - JDiMatteo
3
在我的情况下,LoadLibraryA(somedll.dll) 返回了126。somedll.dll 是存在的,但它需要一些未安装的 someOtherDll.dll。使用 ProcessMonitor 帮助找到了这个问题。 - TomEberhard
1
谢谢!我会编辑答案以添加,在进程监视器中,您可以过滤结果以仅显示您要查找的内容! - Robson
6
我原本对Process Monitor不抱有太大希望,但当我查看加载我的dll时相邻的行时,发现MSVCP140D.dll出现了NAME NOT FOUND的结果。后来发现无法加载我的dll的机器上没有'MSVCP140.dll'的'D'版本。当我为发布构建我的dll时,一切都正常了! - Pakman
显示剩余7条评论

12
这也可能发生在您尝试加载一个需要另一个DLL但无法找到的DLL时。

4

这个错误可能是因为某些MFC库(例如mfc120.dll)所依赖的DLL在windows/system32文件夹中缺失导致的。


0
// 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");
}

1
你的回答可以通过添加更多关于代码的信息以及它如何帮助提问者来改进。 - Tyler2P

0
在我的情况下,这一切都与字符集和加载器函数的形式有关。这是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行时出现了错误。 我发现导致这个问题出现的原因是我的_dll文件中对Java虚拟机动态链接库(dll)的链式加载。在我的情况下,我的_dll文件需要将jvm.dll标记为"延迟加载"。 设置在项目级别进行: 配置属性/链接器/输入/延迟加载的Dlls 在这里我写入了jvm.dll; 这个错误是可以重现的。


1
我在Visual Studio 2022 (17.3.2)上遇到了同样的问题。 在项目中,默认字符集为“Unicode”。 使用此字符集时,库无法加载。错误126。 如果将字符集更改为“多字节(MBCS)”,则可以正常工作。要配置字符集:配置属性>高级参数>字符集。 - Juan

0
在我的情况下,LoadLibrary(..) 中的 dll 名称不正确。

0

将项目属性中的“配置属性”/“C/C++”/“代码生成”/“运行时库”从多线程 DLL(/MD)更改为多线程(/MT)在相同情况下对我有所帮助。


-1

在我的情况下,我从源代码构建了tesseract.dll,但无法在同一构建机器上使用它,因为dll加载失败并显示错误126。Dependency Walker显示缺少msvcp140.dll和vcruntime140.dll。是的,这些dll是通过Visual C++ Redistributable for Visual Studio 2015(https://www.microsoft.com/en-us/download/confirmation.aspx?id=48145)安装的。 - Alexander Samoylov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接