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");
}
}
5
  • 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, 2013 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, 2013 at 15:45
  • 4
    It's not a good practice to call FreeLibrary(dll_mod); under if(dll_mod==NULL)
    – borisbn
    Jan 16, 2013 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, 2013 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, 2013 at 15:51

7 Answers 7

107

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.
12
  • 4
    That's it! With that utility I found the error, thanks so much!
    – Spamdark
    Jan 16, 2013 at 15:59
  • 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 1, 2015 at 1: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. Nov 4, 2015 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, 2017 at 7:27
  • 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, 2018 at 19:57
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.

4

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

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.

1
  • 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, 2022 at 11:40
0

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

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.

-1

This worked for me Visual C++ Redistributable Packages

1
  • 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 (microsoft.com/en-us/download/confirmation.aspx?id=48145). Mar 16, 2020 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.