private void CloseTunDev(IntPtr handle) { if (handle != IntPtr.Zero) { NativeNetif.CloseHandle(handle); } }
protected bool DeviceIoControl(uint commands, byte[] contents) { IntPtr hEvent = NativeNetif.CreateEvent(IntPtr.Zero, false, false, null); try { NativeNetif.OVERLAPPED overlapped = new NativeNetif.OVERLAPPED(); overlapped.hEvent = hEvent; uint dw = 0; uint content_size = 0; if (contents == null) { if (!NativeNetif.DeviceIoControl(this.Handle, commands, contents, 0, contents, 0, ref dw, ref overlapped)) { NativeNetif.WaitForSingleObject(hEvent, Timeout.Infinite); return(overlapped.Internal == NativeNetif.ERROR_SUCCESS); } } else { content_size = (uint)contents.Length; if (!NativeNetif.DeviceIoControl(this.Handle, commands, contents, content_size, contents, content_size, ref dw, ref overlapped)) { NativeNetif.WaitForSingleObject(hEvent, Timeout.Infinite); return(overlapped.Internal == NativeNetif.ERROR_SUCCESS); } } return(true); } finally { if (hEvent != IntPtr.Zero) { NativeNetif.CloseHandle(hEvent); } } }