55

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
  • 37,420
  • 30
  • 139
  • 188
Spamdark
  • 1,341
  • 2
  • 19
  • 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

7 Answers7

106

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://learn.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
  • 52,989
  • 9
  • 71
  • 124
DanS
  • 1,221
  • 1
  • 9
  • 4
  • 4
    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
  • This solution helped me too but I discovered the DLL that I was trying to load was not in the root directory and ProcMon showed the process looking for the DLL in all of my PATH directories. After adding the folder containing my DLL into the PATH variable the assembly loaded just fine. – Loligans Dec 19 '22 at 16:51
12

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,374
  • 2
  • 16
  • 22
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
  • 26,356
  • 27
  • 122
  • 180
0

In my case it was all about character sets v.s. form of loader function. This is visual studio 2019 setting at Project/Properties/Configuration Properties/Advanced/Character Set which has two choices:

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

Today I banged my head again to 126. I learned one thing that makes 126 happen again on top of those previous examples is chained loading of java's virtual machine dll from my_dll. In my case my_dll needs to have jvm.dll marked as "delay loaded". Setting is at project level: Configuration Properties/Linker/Input/Delay Loaded Dlls where I wrote jvm.dll; This error is something I can repeat.

Tonecops
  • 127
  • 9
  • 1
    I had the same issue on Visual Studio 2022 (17.3.2). In the project the default caracter set is "Unicode". With this caracter set the library can't be loaded. Error 126. If you change the caracter set to "Multioctet (MBCS)" it's works. To configure the caracter set : Configuration Property > Advanced Parameters > Caracter Set – Juan Aug 24 '22 at 11:40
0

In my case, the name of the dll inside LoadLibrary(..) was incorrect.

habi
  • 125
  • 8
0

Changing project's property 'Configuration Properties'/'С/С++'/'Code Generation'/'Runtime Library' from Multi-Threaded DLL(/MD) to Multi-Threaded(/MT) helped me in the same case.

Aisilu
  • 1
-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