The problem: normally, you use DLLImport and P/Invoke. However, it only works if DLL is loaded. For normal program you can put it into the same folder as managed executable, and everything will be just fine. Not for ASP.NET, which is compiled into an obscure location, which you would not want to mess up with. Other places like System32 folder are also not a good choice. So, what can you do?

The solution: Actually, people already thought about this problem and found some solutions. See, for example, here. Some people even suggest to create the class dynamically, utting the correct DLLImport attribute value using the reflections. (See. here). However, the reality is much simpler. The only real problem is to ensure that DLL is loaded. Once way to do that, is to create a special folder and put it into the PATH nevironment variable. If you don’t like that, you can use the same solution, I did lately, simply load DLL ahead using LoadLibrary. Once it’s in memory, the system will not look around on disk and will simply use it.

Code fragments (C#):

Declaration of LoadLibrary:

 public static extern IntPtr LoadLibrary(string lpFileName);

 public static extern IntPtr FreeLibrary(IntPtr library);

Declaration of your functions from C++ DLL:

 [DllImport(“SampleLib.dll”, PreserveSig = true, CharSet = CharSet.Unicode)]
 public static extern UInt32 DoStuff(…parameters…);

Loading DLL:

 String DLLPath;
 DLLPath = … full path to your DLL…
 IntPtr lib = LoadLibrary(DLLPath);
 … now, if lib is equal zero, you failed…

Now you can call it:

 UInt32 res = DoStuff(…);

And after you did what you wanted, don’t forget to free it:

 FreeLibrary(lib); // check HRESULT, if that bothers you