C++ DLL 加载库 - 错误代码 126

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14361992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:15:46  来源:igfitidea点击:

DLL Load Library - Error Code 126

c++pluginsdllloadlibrary

提问by Spamdark

I'm using the 'LoadLibrary' from the Windows API, when I run the application, it throws me an error code 126. I read that it may be caused by dependencies, I checked what's wrong with some applications like Dependency Walker, but everything was fine.

我正在使用 Windows API 中的“LoadLibrary”,当我运行应用程序时,它向我抛出错误代码 126。我读到它可能是由依赖关系引起的,我检查了一些应用程序(例如 Dependency Walker)有什么问题,但一切正常很好。

LoadLibrary in the application:

应用程序中的 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();
            }

Plugin code:

插件代码:

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

回答by DanS

Windows dll error 126 can have many root causes. The most useful methods I have found to debug this are:

Windows dll 错误 126 可能有很多根本原因。我发现的最有用的调试方法是:

  1. Use dependency walker to look for any obvious problems (which you have already done)
  2. Use the sysinternals utility Process Monitor http://technet.microsoft.com/en-us/sysinternals/bb896645from Microsoft to trace all file access while your dll is trying to load. With this utility, you will see everything that that dll is trying to pull in and usually the problem can be determined from there.
  1. 使用 Dependency walker 查找任何明显的问题(您已经完成了)
  2. 使用Microsoft的 sysinternals 实用程序 Process Monitor http://technet.microsoft.com/en-us/sysinternals/bb896645来跟踪在您的 dll 尝试加载时的所有文件访问。使用此实用程序,您将看到 dll 试图引入的所有内容,通常可以从那里确定问题。

回答by Iacopo Braccesi

This error can happen because some MFC library (eg. mfc120.dll) from which the DLL is dependent is missing in windows/system32 folder.

发生此错误的原因可能是 windows/system32 文件夹中缺少 DLL 所依赖的某些 MFC 库(例如 mfc120.dll)。

回答by Shivanshu Goyal

This can also happen when you're trying to load a DLL and that in turn needs another DLL which cannot be not found.

当您尝试加载 DLL 而这又需要另一个无法找到的 DLL 时,也会发生这种情况。