Description
This one:
Without that, it cannot be used in return value marshalling because the return value marshaller needs the default constructor to make an instance of one.
Here's an example of a p/invoke that doesn't actually work because of this:
Interop.CertCreateCertificateContext.cs is probably dead code. But I can see that SafeCertContextHandle
is actually used as a return value in a number of p/invokes. It should probably be investigated why they're not failing (or why we have no coverage).
Here's a sample standalone example showing the CertCreateCertificateContext p/invoke not actually working (feel free to paste into an empty console app):
using System;
using System.Runtime.InteropServices;
unsafe
{
Crypt32.CertCreateCertificateContext(0, null, 0);
}
internal static partial class Crypt32
{
[DllImport("Crypt32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern unsafe SafeCertContextHandle CertCreateCertificateContext(
int dwCertEncodingType,
void* pbCertEncoded,
int cbCertEncoded);
}
internal sealed class SafeCertContextHandle : SafeHandle
{
internal SafeCertContextHandle(IntPtr handle) :
base(handle, ownsHandle: true)
{
}
public sealed override bool IsInvalid
{
get { return handle == IntPtr.Zero; }
}
protected sealed override bool ReleaseHandle()
{
//Interop.Crypt32.CertFreeCertificateContext(handle); // CertFreeCertificateContext always returns TRUE so no point in checking.
SetHandle(IntPtr.Zero);
return true;
}
}
The above example will fail with a useless System.MissingMethodException: '.ctor'
, but that's what interop is complaining about.