last day (14 days later) » 

2:01 PM
0
Q: How do I import .dll library from subfolder in C#

sczdavosWhen I have the library in the same folder as app I can simply: [DllImport("kernel32")] public extern static IntPtr LoadLibrary(string librayName); IntPtr iq_dll = LoadLibrary("IQPokyd.dll"); I also have this in my app.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime>...

 
Is the IQPokyd.dll a .NET assembly? Or it is a native DLL?
 
It's 3th party dll made in C++ I think. So I need to import it this way.
I've rewritten the question.
 
In this case LoadLibrary with the full path should help. I know you tried. If so, I believe the error is somewhere else.
 
When I tried use relative or absolute path program won't even start it throw fatal error. But with just name of library it will pass and error will come after method call. Everything work fine if not in plugins folder.
 
Please show a code with the full path you tried.
 
2:01 PM
I've tried this: IntPtr iq_dll = LoadLibrary("plugins\\IQPokyd.dll"); And this: LoadLibrary(System.Windows.Forms.Application.StartupPath + "\\plugins\\IQPokyd.dll"); It should be correct. I've find out that if there is just "IQPokyd" value of iq_dll is 0. With "plugins\\..." or absolute path there is some value. But it will show me this: dropbox.com/s/5ley1ahlbyg211s/…
 
What does GetLastError say?
 
Currently I have dll in plugins folder and I'm loading it with "plugins\\my.dll". GetLastError method called in main wrote "2".
 
2:17 PM
Try to specify the full path: C:\\...something...\\plugins\\IQPokyd.dll
 
You mean instead of Application.StartupPath enter it manually?
Or just do the same with absolute path
 
Yup, just for test purpose.
 
It's the same as with relative
If the DLL is found (so the path is correct) I get that error window. Is the path is wrong it will not show error window but value of variables are zeros "0". So if I try to use them it will give me an exception.
So I think I'm able to load that DLL but there is some inner problem. And I'm not sure what to do with this :/
 
The `GetLastError` error codes: `2 File was not found`.
When path is correct, there should be another error code.
 
What if this error does not belong to library I'm trying to load but to some file the library is trying load?
Is this possible?
Yeah this is it. There is some config file with which the library works. If the library is in plugins folder and that config file is in folder with program it works. Is there another possibility then rewriting that library code?
 
2:31 PM
You could try to temporarily change the current directory before referencing that DLL.
 
Hey!!
It actually works
Directory.SetCurrentDirectory(Global.appRunPath + "plugins");
Should I change it in the end of method to Directory.SetCurrentDirectory(Global.appRunPath);?
 
I don't think it is necessary, but who knows...
 
I'll rather do it :) Thanks a lot buddy. Will you post the solution we've find or should I do that?
 
I'll do it.
 
Ok, I'll mark it as correct. Thanks again for your time. Have a nice day, cya :)
 
2:50 PM
Done. Have a nice day too. :)
 

last day (14 days later) »