Question Problem loading dll on Windows 11.

rkb771

New member
Joined
Jun 21, 2022
Messages
4
Programming Experience
Beginner
Simply put, I have a c# code for UI and a native c++ dll for calculation. The dll is compiled for 64-bit with gcc 11.2. I am using MSVS 2022 for the UI. Selected .NET version is 4.8. The import is done with LoadLibrary, GetProcessAdress and Delegate.

The c++ functions are exported as,
C++:
extern "C"{
    __declspec(dllexport) ReturnType __stdcall FunctionName(Arguments)
    {
        //body
    }
}

The c# code is
C#:
IntPtr module = LoadLibrary(DllFilePath);
if(IntPtr == null)
{
    int ErrorCode = Marshal.GetLastWin32Error();
}

On my laptop, running windows 10, the UI runs fine. Loads the dll, imports all function, calls those functions, gets returns. I can run it on a different PC too (Windows 10). But when I sent the app to my boss, she couldn't run it. Her PC is running Windows 11 and has VS 2022 installed. She tried compiling and running the source code and got error at dll import. I checked the project clone in her PC which is exactly the same as mine. There is nothing wrong with path. But LoadLibrary returns null and the ErrorCode is 126. I tried installing Windows 10 and 11 SDKs, didn't help.
I also supplied the known dependencies "libgcc", "libstdc++", "libwinpthread". Using Dependency Walker, I can't find any other dependency that could be missing from windows other than these three.

My guess is that Windows 11 is missing some dependency I don't know of. But I couldn't find any missing dependency or solution online. It would be great help if someone could point out what could be going wrong.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,468
Location
Sydney, Australia
Programming Experience
10+
She tried compiling and running the source code and got error at dll import.
If only there was some way for us to know what code was being executed and what the error message was.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
Sounds like an issue in the building of the C++ component, and not really a C# problem. Anyway, the trick is to run Dependency Walker on the C++ DLL to see what dependencies it is missing.
 

rkb771

New member
Joined
Jun 21, 2022
Messages
4
Programming Experience
Beginner
Sounds like an issue in the building of the C++ component, and not really a C# problem. Anyway, the trick is to run Dependency Walker on the C++ DLL to see what dependencies it is missing.
I built the c++ component with "-std=c++20 -O3 -m64" flags. Using Dependency Walker, other than the "API-something" and "EXT-something" dlls, it only uses the 3 dlls mentioned in the question. I have put all three in the same folder as my dll. Is there any component from Windows 10 that is not included in Windows 11?
 

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
105
Programming Experience
10+
The error code 126 suggests it is trying to load some other DLL (which may or may not be a dependency) but unable to find it. Try running Process Monitor on your process and see if you can spot any FILE NOT FOUND errors or such.
 

rkb771

New member
Joined
Jun 21, 2022
Messages
4
Programming Experience
Beginner
The problem was neither with the native module nor a missing dependency. I packed the module and its dependencies in a folder in the same path as the executable. The c# code, however, ignored the folder when looking for dependency. It finds my dll, tries to load it, needs some other dlls and looks in the root path and other system paths. But not the path of my dll. That is where the dependencies were.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
But it's not the C# code that loads your DLL. You are just calling the Win32 API LoadLibrary() based on the code and descriptions you have given above. The behavior you described in post #7 is the standard behavior of that API.
 
Top Bottom