DllImport with out SafeHandle results in MissingMethodException
See the question and my original answer on StackOverflowYou must provide a public parameterless constructor to a class that derives from SafeHandle, especially when you it with p/invoke, as defined here: SafeHandle
Your subclass of SafeHandle is only required to provide three methods
.ctor() – A default constructor that initializes the SafeHandle. This method is used by P/Invoke when it returns a SafeHandle to your process
bool IsInvalid { get; } – a property to determine if the current value of the handle is valid or not
bool ReleaseHandle() – clean up the contained resource
p/invoke will set the value magically anyway. It's also in the official documentation:
When you inherit from SafeHandle, you must override the following members: IsInvalid and ReleaseHandle. You should also provide a default constructor that calls the base constructor with a value that represent an invalid handle value, and a Boolean value indicating whether the native handle is owned by the SafeHandle and consequently should be freed when that SafeHandle has been disposed.
Since SafeHandleZeroOrMinusOneIsInvalid
doesn't define a public parameterless constructor, you must do it.