Search code examples
c#pinvoke

DllImport, how to check if the DLL is loaded?


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.


Solution

  • 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.