Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DllImport, how to check if the DLL is loaded?

Tags:

c#

pinvoke

I am doing a P/Invoke, and I am using the following method

[DllImport("Authz.dll", SetLastError = true)]
    public static extern BOOL AuthzFreeContext(
        IntPtr phAuthzResourceManager);

even though its working, how is it guaranteed that Authz.dll is always loaded into my code. Suppose my dll is some XXX.dll how should I check in general if that dll is loaded or not before using that, so that I don't get a method not found exception.

like image 472
sri Avatar asked Aug 06 '12 11:08

sri


1 Answers

Marshal.PrelinkAll(Type)

or

Marshal.Prelink(MethodInfo)

Sadly, the documentation fails to mention any exceptions being thrown if the DLL is not found. I have just verified via a simple app that it is indeed a DllNotFoundException being thrown.

like image 52
leppie Avatar answered Nov 06 '22 11:11

leppie