Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I already loaded a dll from a manually created folder inside program files, by referencing to it from solution explorer. And it worked from VS !
But i did a quick test, by moving my executable in another folder and run it again. It gives me errors, but if i put the dll with the executable, is working fine again. Also, i looked inside Debug folder and the dll appeared there automatically after running the program from VS. So... my impression is, after i also read about this fenomenon on internet, it seems it has to do with the Debugger inside VS or something.
i also find this post that give me a general idea about the issue...
c# - Dynamically load a DLL from a specific folder? - Stack Overflow[^]

I wonder if there is an easy solution or is as complicated as i already find it?
I will implement what i find, but i want your opinion first.
Its a bit advanced what i find there,that's why im hesitating.
Thank you for your help.(in advance)

What I have tried:

C#
//at this point i get an error if the dll is missing from folder.
InitializeComponent();
Posted
Updated 19-Sep-19 21:18pm
v2

1 solution

This is not a Visual Studio issue. At execution time the loading of dlls is under the control of the Windows OS, controlled by the search rules:
- the directory where the ,exes file was loade.
- the system library directory.
- any directories listed in the DLL search path - see LoadLibraryA function (libloaderapi.h) | Microsoft Docs[^].

See also Dynamic-Link Library Search Order - Windows applications | Microsoft Docs[^].
 
Share this answer
 
v2
Comments
_Q12_ 20-Sep-19 6:05am    
ok, thank you, but how to write it?
I find this so far:

public partial class Form1 : Form, IMessageFilter
{

//is loading but the dll are not found
[DllImport("kernel32")]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

// error on LoadLibraryFlags
//[DllImport("kernel32.dll", SetLastError = true)]
//static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

//error on LPCSTR
//[DllImport("kernel32.dll")]
//static extern IntPtr LoadLibraryA(LPCSTR lpLibFileName);


public Form1()
{

string dllA = @"c:\Program Files (x86)\VideoList\AxInterop.WMPLib.dll";
string dllB = @"c:\Program Files (x86)\VideoList\Interop.WMPLib.dll";

LoadLibrary(dllA);
LoadLibrary(dllB);

//LoadLibraryEx(dllA, IntPtr.Zero, 0);
//LoadLibraryEx(dllB, IntPtr.Zero, 0);

//LoadLibraryA(dllA);
//LoadLibraryA(dllB);
_Q12_ 20-Sep-19 6:33am    
That LPCSTR is a string from what i could find so far.
So my earlier code, now is:
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibraryA(string lpLibFileName);

BUT, the dll's are still not found, error in the code at the expression that uses that dll.
_Q12_ 20-Sep-19 7:14am    
ive made a new folder and put there my dll:
string dllA = @"c:\VideoList\AxInterop.WMPLib.dll";
string dllB = @"c:\VideoList\Interop.WMPLib.dll";
i thought maybe the root having spaces in the name of the folder may create my loading error.
But no change, the same error that it can't find them.
Richard MacCutchan 20-Sep-19 10:16am    
You need to use LoadLibraryW as C* strings are UniCode,
_Q12_ 20-Sep-19 18:56pm    
i just catch a little 'bug' from loading the dll. When I Reference the dll's from solution explorer, i still got an error (but different than before). So i looked in the dll properties and set it's "Embed Interop Type" to False. And the application is successfully running. If i delete again the reference dll's and change my code with that property?...maybe that should be done, but i dont know how to write the code for that property. Maybe you can help me. And thank you so much for your help until here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900