10

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.

sri
  • 1,005
  • 1
  • 12
  • 26
  • You can P/Invoke on loadlibrary and see if that succeeds? http://www.pinvoke.net/default.aspx/kernel32.loadlibrary – rene Aug 06 '12 at 11:58

1 Answers1

15

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
leppie
  • 115,091
  • 17
  • 196
  • 297