1

First i will explain my situation. I have a DLL library that is a black box to me. I don't know for sure in what language was it written. My guess it is C++ or even in C, but i don't know. But i DO know that DLL functions so I know how to use it.

Ok. Straight to businesses. I'm writing application C# .NET that will work with that DLL mentioned above. I tried all know to me linking methods but only one works:

  • Static: [DllImport("myDll.dll")] - That works.
  • Dynamic: by Kernel32.dll LoadLibrary/FreeLibrary - I'm getting exception with code 126 - The specified module could not be found. That problem lays in missing dependence.
  • Dynamic: by Reflection - When using Assembly.LoadFile(aPathFileName), I'm getting exception: The module was expected to contain an assembly manifest.

My problem is, that i really need to specify path to my black box dll in some sort of configuration file. Can I ask for any tip what I can do now, how i can resolve this problem? More specific: Is there ANY way to load string from config file and use it to fill constant string, required by static linking DllImport(const string path); or there is another way to dynamic link dll that i don't know?

EDIT

Answer to possible duplicate of another question: Both answers of that question reference to methods i can't use. As i said, i can't use LoadLibrary (exception 126) and also i can't specify loot of possible paths hardcoded (i said that i need load path from config file)

user1916778
  • 137
  • 1
  • 4
  • 13
  • This other post may work! I can invoke kernel32.dll SetDllDirectory and fill it with path from config file, before calling method of my black box dll, linked by static DllImport! I'll try it and post proper answer. – user1916778 Apr 12 '16 at 09:17

1 Answers1

3

Sinatr posted link to this question, where Cody Gray gave an interesting solution. Static DllImport target path to dll can be override by kernel32.dll SetDllDirectory. All i needed to do was invoke this kernel function

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);

And then fill it with path saved in my configuration file BEFORE first use of target dll function.

SetDllDirectory(Settings.Default.dllPath);

Important is to set for SetDllDirectory ONLY path to folder where target dll is present (like c:\folder), not full path with name of the dll (like c:\folder\name.dll).

Community
  • 1
  • 1
user1916778
  • 137
  • 1
  • 4
  • 13