For an application that uses the WinUsb API, I'm attempting to use a handle from a derived SafeHandle class in place of an IntPtr:
Passing _winUsbHandle in the InterfaceHandle parameter of WinUsb_Initialize returns the error "SafeHandle cannot be null".
Setting:
How can I use a SafeHandle with this function?
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)] [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] internal class SafeWinUsbHandle : SafeHandle { private SafeWinUsbHandle() : base(IntPtr.Zero, true) { } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] protected override bool ReleaseHandle() { } public override bool IsInvalid { get { if (handle == IntPtr.Zero) { return true; } if (handle == (IntPtr) (-1)) { return true; } return false; } } private SafeWinUsbHandle _winUsbHandle;
Passing _winUsbHandle in the InterfaceHandle parameter of WinUsb_Initialize returns the error "SafeHandle cannot be null".
Setting:
_winUsbHandle = IntPtr.Zero;returns the error "Cannot convert source type System.IntPtr to target type".
How can I use a SafeHandle with this function?