3

When 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>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="plugins;" />
    </assemblyBinding>
  </runtime>
</configuration>

I'm able to load this library. Problem is that it is using some config files which needs to be in app run directory. Is there some possibility how to tell the library that files it needs are in the same folder as the library?

sczdavos
  • 2,035
  • 11
  • 37
  • 71
  • Is the `IQPokyd.dll` a .NET assembly? Or it is a native DLL? – Dmitry Oct 12 '14 at 13:37
  • It's 3th party dll made in C++ I think. So I need to import it this way. – sczdavos Oct 12 '14 at 13:38
  • I've rewritten the question. – sczdavos Oct 12 '14 at 13:42
  • In this case `LoadLibrary` with the full path should help. I know you tried. If so, I believe the error is somewhere else. – Dmitry Oct 12 '14 at 13:43
  • 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. – sczdavos Oct 12 '14 at 13:45
  • Please show a code with the full path you tried. – Dmitry Oct 12 '14 at 13:49
  • 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: https://www.dropbox.com/s/5ley1ahlbyg211s/Screenshot%202014-10-12%2015.53.06.png?dl=0 – sczdavos Oct 12 '14 at 13:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62923/discussion-between-dmitry-and-sczdavos). – Dmitry Oct 12 '14 at 14:01
  • You are playing a losing game, very important for both LoadLibrary and the function you are calling to use *full* path names. Like c:\foo\bar\baz.ext, using baz.ext or bar\baz.ext causes entirely too much trouble with Environment.CurrentDirectory. Including the fopen() call that failed in that DLL. – Hans Passant Oct 12 '14 at 14:08
  • How can I call the function with full path name? – sczdavos Oct 12 '14 at 14:13
  • I've found out that library will load but it is using some config files for which is used run path. So if I move them to app directory it works. – sczdavos Oct 12 '14 at 14:30
  • You should still be able to load the config files manually, as I've [explained here](http://stackoverflow.com/a/5191101/447356). – Shadow The GPT Wizard Oct 12 '14 at 14:55

2 Answers2

2

Since the DLL searches for the config in the current directory, it makes sense to temporarily change the current directory before loading it:

string saveCurDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.Combine(Application.StartupPath, "plugins"));

IntPtr iq_dll = LoadLibrary("IQPokyd.dll");

Directory.SetCurrentDirectory(saveCurDir);
Dmitry
  • 13,797
  • 6
  • 32
  • 48
0

You can do this by adding a probing tag to your app's .config file. For example, if you wanted libraries to be loaded from a /lib/ subdirectory, your config would look like this:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="lib"/>
    </assemblyBinding>
  </runtime>
</configuration>
Michael K.
  • 74
  • 7