50

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.

LoadLibrary in the application:

            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");
}
}
Jean-François Corbett
  • 36,032
  • 27
  • 135
  • 183
Spamdark
  • 1,271
  • 2
  • 17
  • 38
  • what platform are you programming on? i just typed "LoadLibrary failed" on Google and it autocompleted immediately with "LoadLibrary failed with error code 126", giving something like 41.000 results, including YouTube videos on how to fix it. isn't really any of those links useful? – Andy Prowl Jan 16 '13 at 15:39
  • I followed some tutorials, they talked about the dependencies... about that the dll does not exists, well, I have like 4 hours searching and I can't fix it with any tutorial, I already checked the dependencies.... :S – Spamdark Jan 16 '13 at 15:45
  • 4
    It's not a good practice to call `FreeLibrary(dll_mod);` under `if(dll_mod==NULL)` – borisbn Jan 16 '13 at 15:47
  • 3
    are you sure you are not trying to load a 64 bit library from a 32 bit executable? or vice versa? or that your 32 bit DLL does not depend, directly or indirectly, on 64 bit libraries? or vice versa? have you checked that your DLL is visible to your application? where is the DLL file located and where is the EXE file located? – Andy Prowl Jan 16 '13 at 15:49
  • 3
    And another moment: you should call `GetLastError` straight away after `LoadLibrary`, because `stringstream`'s constructor and it's operator << could (potentionaly) call some WinAPI, that will drop last error into zero – borisbn Jan 16 '13 at 15:51

4 Answers4

96

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

  1. Use dependency walker to look for any obvious problems (which you have already done)
  2. Use the sysinternals utility Process Monitor https://docs.microsoft.com/en-us/sysinternals/downloads/procmon from 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.
Eric Duminil
  • 50,694
  • 8
  • 64
  • 113
DanS
  • 1,121
  • 8
  • 4
  • 3
    That's it! With that utility I found the error, thanks so much! – Spamdark Jan 16 '13 at 15:59
  • @Spamdark: we're glad for you. Please post what was the reason – Andriy Tylychko Jan 16 '13 at 16:15
  • It was a dependency, I don't know why the "dependency walker" didn't detect or didn't throw any error. It's working fine now :) – Spamdark Jan 16 '13 at 16:17
  • 18
    Unfortunately often dependency walker fails to show missing dependencies, and also often shows false positives. A few years ago it seemed much more reliable -- perhaps it isn't keeping up with recent operating systems very well. – JDiMatteo Jan 01 '15 at 01:00
  • 3
    in my case, LoadLibaryA(somedll.dll) returned with 126. somedll.dll was there, but it needed someOtherDll.dll, which was not installed. ProcessMonitor helped find that issue. – TomEberhard Nov 04 '15 at 22:09
  • 1
    thanks! I would edit the answer to add that in process monitor you can filter the results to show only what you are looking for! – Robson May 31 '17 at 07:27
  • Nice answer. Adding a link to the **Dependency Walker help page** with some descriptions of such potential " [**hidden depencencies**](http://www.dependencywalker.com/help/html/dependency_types.htm)" - in particular see items 4 and 5. – Stein Åsmul Dec 13 '17 at 11:38
  • 6
    I was dubious Process Monitor would reveal anything, but when I looked at the rows surrounding my dll being loaded I saw MSVCP140D.dll was giving a result of NAME NOT FOUND. Turns out the machine that couldn't load my dll doesn't have the 'D' version of MSVCP140.dll. Everything worked when I built my dll for release! – Pakman Oct 18 '18 at 19:57
  • I used process monitor, but the event is showing that it is trying to load some dll with japanese name. I am using Qt creator. `D:\thomas\works\qt\2\build-test1-Desktop_Qt_5_11_1_MSVC2017_64bit-Debug\debug\㩄琯潨慭⽳潷歲⽳瑱㈯戯極摬琭獥ㅴ䐭獥瑫灯兟彴張ㄱㅟ䵟噓㉃㄰強㐶楢⵴敄畢⽧敤畢⽧牃䥤挲㈳搮汬.DLL` – thomachan Feb 01 '19 at 12:36
  • Dependency Walker worked like a charm. Thanks for reminding me of its existence! – hidefromkgb Jul 22 '19 at 12:26
  • Wow, *ProcessMonitor* is really impressive. It worked like a charm, and I could understand what went wrong with DLL loading, and solve the problem in 2 minutes. I've also noticed that somehow, Adobe needs to check something every 100ms on my computer, even though no Adobe app has been launched. WTF. – Eric Duminil Dec 14 '21 at 14:58
9

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

Shivanshu Goyal
  • 1,227
  • 1
  • 15
  • 21
4

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

bluish
  • 24,718
  • 26
  • 114
  • 174
-1

This worked for me Visual C++ Redistributable Packages

QuerSlider
  • 21
  • 4
  • In my case I built tesseract.dll from source and could not use it on the same build machine, because the dll load failed with error 126. Dependency Walker showed msvcp140.dll and vcruntime140.dll as missing. Yes, these dlls are installed with Visual C++ Redistributable for Visual Studio 2015 (https://www.microsoft.com/en-us/download/confirmation.aspx?id=48145). – Alexander Samoylov Mar 16 '20 at 13:50