What happens if / when DllImport is called multiple times?

I download .dll written in Delphi 7 using DllImport in a Windows service written in C # .NET 4. Before deploying this service, I just want to make sure that I don’t need to do anything specifically to handle unmanaged .dll.

My C # code looks something like this:

[DllImport("MyDelphiDLL.dll")] private static extern string DoSomething(string value); private void SomeMethod(List<string> values) { foreach (string value in values) { string newValue = DoSomething(value); } } 

The DoSomething function will be called several times, and I suspect that MyDelphiDLL.dll is loaded either when the managed DLL is loaded, or the first link to DoSomething, but I'm not sure.

I looked at the DllImportAttribute Class documentation on MSDN, but actually it is not anyway. I also searched for SO, and Googled the question every way I can think of, and still have not found a definitive answer.

I just want to make sure everything is correct.

+4
source share
2 answers

No problem with what you do. The DLL will be loaded once and will remain loaded.

+4
source

I think your dll will be loaded once. DllImport is just a point for the compiler to generate code for the DoSomething method in a special way, and not to compile the body it method in IL.

Also, in a deep base, it seems to call the LoadLibrary WinAPI method, which loads the dll only once. I am not sure if it is possible to load the dll several times within the same process.

+4
source

All Articles