Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

How to use a SafeHandle with the WinUSB API

$
0
0
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:

		[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?



Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>