Loading a DLL explicitly?

  • Thread starter Thread starter Michi Henning
  • Start date Start date
M

Michi Henning

I've been looking at the doc to find the answer to this but
didn't get very far. Assembly.LoadFile() seems to be close,
but not quite the right thing...

I have a DLL containing code written in C. In my C# code,
I want to check whether the DLL exists and, if so, load it.
Otherwise, if the DLL isn't there, the C# code can make do without it.
(The DLL contains optional functionality.)

In other words, I'd like to do explictly what is done implicitly
by [DllImport("MyLib.dll")] and check whether the DLL could be loaded.

Can anyone give me a hand with this?

Thanks,

Michi.
 
I think invoking a non-existent DllImport function just raises an exception.
Couldn't you simply catch that?
If that doesn't work, you can still do it the good old way: PInvoke
LoadLibrary and GetProcAddress.

Niki
 
Hi,

I think this can be done with some trickery:

1. Reference the LoadLibrary and UnloadLibrary APIs with [DllImport]. These
are AFAIR in kernel32.dll and are therefore always available
2. Try to load the DLL with LoadLibrary. If the call succeeds, the DLL is
available so you can set an internal flag
3. Use the functions imported from that DLL only if the internal
availability flag. I assume here the framework won't check for the DLL
presense unless its imported functions are actually used.
4. When the program exits, call UnloadLibrary to free up the DLL.
 
Niki said:
I think invoking a non-existent DllImport function just raises an exception.
Couldn't you simply catch that?

Yes, but it's not all that convenient, because there are many code paths
into that DLL. Catching the exception is possible, but a bit messy. I'd
prefer to find out up front whether the library is available and set a flag.
If that doesn't work, you can still do it the good old way: PInvoke
LoadLibrary and GetProcAddress.

Yes, looks like that's the way to go, thanks!

Cheers,

Michi.
 
Dmitriy said:
Hi,

I think this can be done with some trickery:

1. Reference the LoadLibrary and UnloadLibrary APIs with [DllImport]. These
are AFAIR in kernel32.dll and are therefore always available
2. Try to load the DLL with LoadLibrary. If the call succeeds, the DLL is
available so you can set an internal flag
3. Use the functions imported from that DLL only if the internal
availability flag. I assume here the framework won't check for the DLL
presense unless its imported functions are actually used.
4. When the program exits, call UnloadLibrary to free up the DLL.

Right, that sounds like the way to go. Thanks muchly!

Cheers,

Michi.
 
Michi Henning said:
Yes, but it's not all that convenient, because there are many code paths
into that DLL. Catching the exception is possible, but a bit messy. I'd
prefer to find out up front whether the library is available and set a
flag.

Counldn't you just invoke one dummy function early in your application, and
set a flag if no exception was thrown?

Niki
 
Back
Top