Issues loading DLL

I'm trying to load a C++ DLL and use it's function GetEngineVersion to get the engine version to find out installed version of the game. I need it to be loaded from Properties.Settings.Default.InstallPath, "LS3DF.dll". I get an error 126 and can't fix it. I can use the DLL function if i use [DllImport] but that only uses fixed path. I need it to be dynamic and load the path from InstallPath setting. i'm using .NET Framework 4.7.2, platform is set to x86 here is a code for a button i use for testing:
private void Button_Click(object sender, EventArgs e)
{
try
{
// find dll
string dllPath = System.IO.Path.Combine(Properties.Settings.Default.InstallPath, "LS3DF.dll");

// load dll
IntPtr hModule = LoadLibrary(dllPath);
if (hModule == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show("Failed to load the DLL.");
return;
}

// pointer
IntPtr pAddressOfFunction = GetProcAddress(hModule, "GetEngineVersion");
if (pAddressOfFunction == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show("Failed to get the address of the function.");
return;
}

// delegate
var getEngineVersion = (GetEngineVersionDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunction, typeof(GetEngineVersionDelegate));

// call function
int engineVersion = getEngineVersion();
System.Windows.Forms.MessageBox.Show($"Engine Version: {engineVersion}");
}
catch (DllNotFoundException ex)
{
System.Windows.Forms.MessageBox.Show($"DLL not found: {ex.Message}");
}
catch (EntryPointNotFoundException ex)
{
System.Windows.Forms.MessageBox.Show($"Method not found in DLL: {ex.Message}");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($"An error occurred: {ex.Message}");
}
}
private void Button_Click(object sender, EventArgs e)
{
try
{
// find dll
string dllPath = System.IO.Path.Combine(Properties.Settings.Default.InstallPath, "LS3DF.dll");

// load dll
IntPtr hModule = LoadLibrary(dllPath);
if (hModule == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show("Failed to load the DLL.");
return;
}

// pointer
IntPtr pAddressOfFunction = GetProcAddress(hModule, "GetEngineVersion");
if (pAddressOfFunction == IntPtr.Zero)
{
System.Windows.Forms.MessageBox.Show("Failed to get the address of the function.");
return;
}

// delegate
var getEngineVersion = (GetEngineVersionDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunction, typeof(GetEngineVersionDelegate));

// call function
int engineVersion = getEngineVersion();
System.Windows.Forms.MessageBox.Show($"Engine Version: {engineVersion}");
}
catch (DllNotFoundException ex)
{
System.Windows.Forms.MessageBox.Show($"DLL not found: {ex.Message}");
}
catch (EntryPointNotFoundException ex)
{
System.Windows.Forms.MessageBox.Show($"Method not found in DLL: {ex.Message}");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($"An error occurred: {ex.Message}");
}
}
8 Replies
SteveTheMadLad
It may be that the DLL is compiled for x64, or has dependencies of its own that can't be found and loaded (possibly because they're located somewhere Windows doesn't look). I haven't used it in ages, but I think you can still find a tool called Dependency Walker, it would tell you what other DLLs a given DLL depends on. Give it your DLL and see what it says. I don't know much about native win32 programming, but while you do specify the full path to the main DLL, if it has dependencies, the OS is going to look for them according to some default logic. There are WinAPI functions such as SetDllDirectory and AddDllDirectory that you can use to control where the system looks for libraries, and even LoadLibraryEx which accepts flags to further control this. Frankly, this is the sort of task where I think ChatGPT or some other LLM can help you out reasonably well.
Richard01_CZ
Richard01_CZOP4d ago
I tried chatGTP to help with the issue but i get stuck in a loop of "include more debugging messags to check if path is correct". Path is correct, dll is there, dll is not corrupted, dll is c++ x86 from 2002 i got a code from some other person that needed the same thing so i adapted their code for my usage and i still get error 126 and when i use the [DllImport] it works
SteveTheMadLad
Gosh, that’s annoying. Have you also tried making sure that DLL doesn’t have any dependencies?
Richard01_CZ
Richard01_CZOP4d ago
i don't really know how i can tell that it doesn't need any using the tool
MODiX
MODiX4d ago
Please don't upload any potentially harmful files @Richard01_CZ, your message has been removed
Richard01_CZ
Richard01_CZOP4d ago
whoops, wanted to share the dll
Richard01_CZ
Richard01_CZOP4d ago
This is the code i have that should work but i still get error 126
Richard01_CZ
Richard01_CZOP3d ago
i used chat gpt to make some basic broken code, fixed it and it seems to be working apart from the dll failing to load I've also checked some other users that had the same issue and the solution is basically the same as i have it here only thing i didn't try is running the app as admin nope, no luck figured it out the DLL was loading successfully but it was looking for dependencies in the application directory, not in the directory the DLL is placed in by calling the function SetDllDirectory(installPath); from imported kernel dll, it was able to load and function correctly installPath being a string from Properties.Settings.Default.InstallPath as that's where i store user selected game directory.
Want results from more Discord servers?
Add your server