public void RegisterDeviceNotification(Window window) { //get main window handle and hook its message events var source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle); IntPtr windowHandle = source.Handle; source.AddHook(HwndHandler); //create filter to find bluetooth WmDevicechange messages only var radioParams = new NativeImports.BLUETOOTH_FIND_RADIO_PARAMS(); radioParams.Initialize(); IntPtr hRadio; IntPtr hFind = NativeImports.BluetoothFindFirstRadio(ref radioParams, out hRadio); if (hRadio != IntPtr.Zero) { do { DEV_BROADCAST_HANDLE filter = new DEV_BROADCAST_HANDLE(); filter.dbch_size = Marshal.SizeOf(filter); filter.dbch_handle = hRadio; filter.dbch_devicetype = DBT_DEVTYP.HANDLE; filter.dbch_eventguid = EMPTY; //send messages of these filtered events to main window notificationHandle = RegisterDeviceNotification(windowHandle, ref filter, DEVICE_NOTIFY.WINDOWS_HANDLE); NativeImports.CloseHandle(hRadio); } while (NativeImports.BluetoothFindNextRadio(ref hFind, out hRadio)); NativeImports.BluetoothFindRadioClose(hFind); } }
/// <summary> /// New version which gets the handle automatically for specified directory /// Only for registering! Unregister with the old version of this function... /// </summary> /// <param name="register"></param> /// <param name="dirPath">e.g. C:\\dir</param> private void RegisterForDeviceChange(string dirPath) { IntPtr handle = Native.OpenDirectory(dirPath); if (handle == IntPtr.Zero) { mDeviceNotifyHandle = IntPtr.Zero; return; } else { mDirHandle = handle; // save handle for closing it when unregistering } // Register for handle DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE(); data.dbch_devicetype = DBT_DEVTYP_HANDLE; data.dbch_reserved = 0; data.dbch_nameoffset = 0; //data.dbch_data = null; //data.dbch_eventguid = 0; data.dbch_handle = handle; data.dbch_hdevnotify = (IntPtr)0; int size = Marshal.SizeOf(data); data.dbch_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, buffer, true); mDeviceNotifyHandle = Native.RegisterDeviceNotification(mRecipientHandle, buffer, 0); }
private static DeviceEventInfo TransformDeviceEvent(ref Message m) { var tm = new DeviceEventInfo { DEVICEEVENT = (ServicesAPI.SERVICE_CONTROL_DEVICEEVENT_Control)(int) m.WParam }; DEV_BROADCAST_HDR dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); tm.DeviceType = dbh.dbch_devicetype; switch (dbh.dbch_devicetype) { case dbch_devicetype.DBT_DEVTYP_DEVICEINTERFACE: DEV_BROADCAST_DEVICEINTERFACE dbdi = (DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE)); tm.DeviceId = cstr_to_string(dbdi.dbcc_name); tm.DeviceName = ConvertDbccNameToFriendlyName(cstr_to_string(dbdi.dbcc_name)); break; case dbch_devicetype.DBT_DEVTYP_HANDLE: DEV_BROADCAST_HANDLE dbbh = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); tm.DeviceName = "Handle Id " + dbbh.dbch_handle.ToString(); break; case dbch_devicetype.DBT_DEVTYP_OEM: DEV_BROADCAST_OEM dbo = (DEV_BROADCAST_OEM)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_OEM)); tm.DeviceName = string.Format("OEM: {0} Value: {1}", dbo.dbco_identifier, dbo.dbco_suppfunc); break; case dbch_devicetype.DBT_DEVTYP_PORT: DEV_BROADCAST_PORT dbp = (DEV_BROADCAST_PORT)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_PORT)); IntPtr pData = (IntPtr)(m.LParam.ToInt32() + dbp.dbcp_size); // (*1) IntPtr offsetDbcpName = Marshal.OffsetOf(typeof(DEV_BROADCAST_PORT), "dbcp_name"); int len = (int)pData - (int)offsetDbcpName; tm.DeviceName = Marshal.PtrToStringAuto(offsetDbcpName, len); break; case dbch_devicetype.DBT_DEVTYP_VOLUME: DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); tm.DeviceName = dbv.dbcv_unitmask.ToString(); if (dbv.dbcv_flags != dbcv_flags.None) { tm.DeviceName += " " + dbv.dbcv_flags.ToString(); } break; } //dbh.dbch_devicetype == dbch_devicetype. //IntPtr pData = (IntPtr)(m.LParam.ToInt32() + Marshal.SizeOf(ps)); // (*1) return(tm); }
void RegisterForNotif(char driveletter) { IntPtr hDrive = GetHDrive(driveletter); DEV_BROADCAST_HANDLE filter = new DEV_BROADCAST_HANDLE(); filter.dbch_size = Marshal.SizeOf(filter); filter.dbch_devicetype = (int)DevTypes.DBT_DEVTYP_HANDLE; filter.dbch_handle = hDrive; IntPtr pfilter = Marshal.AllocHGlobal(Marshal.SizeOf(filter)); Marshal.StructureToPtr(filter, pfilter, true); IntPtr hRDN = RegisterDeviceNotification(frm.Handle, pfilter, (int)DevNotify.DEVICE_NOTIFY_WINDOW_HANDLE); }
/// <summary> /// Registers to be notified when the volume is about to be removed /// This is requierd if you want to get the QUERY REMOVE messages /// </summary> /// <param name="register">true to register, false to unregister</param> /// <param name="fileHandle">handle of a file opened on the removable drive</param> private void RegisterForDeviceChange(bool register, SafeFileHandle fileHandle) { if (register) { // Register for handle DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE { dbch_devicetype = DBT_DEVTYP_HANDLE, dbch_reserved = 0, dbch_nameoffset = 0, //data.dbch_data = null; //data.dbch_eventguid = 0; dbch_handle = fileHandle.DangerousGetHandle(), //Marshal. fileHandle; dbch_hdevnotify = (IntPtr)0 }; int size = Marshal.SizeOf(data); data.dbch_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, buffer, true); mDeviceNotifyHandle = Native.RegisterDeviceNotification(mRecipientHandle, buffer, 0); } else { // close the directory handle if (mDirHandle != IntPtr.Zero) { Native.CloseDirectoryHandle(mDirHandle); // string er = Marshal.GetLastWin32Error().ToString(); } // unregister if (mDeviceNotifyHandle != IntPtr.Zero) { Native.UnregisterDeviceNotification(mDeviceNotifyHandle); } mDeviceNotifyHandle = IntPtr.Zero; mDirHandle = IntPtr.Zero; mCurrentDrive = ""; if (mFileOnFlash != null) { mFileOnFlash.Close(); mFileOnFlash = null; } } }
internal void btRegister(IntPtr bluetoothRadioHandle) { Debug.Assert(_hDevNotification == null, "btRegister, already set."); Debug.Assert(_hDevNotification == null || _hDevNotification.IsInvalid, "btRegister, already registered."); IntPtr windowHandle = this.Handle; DEV_BROADCAST_HANDLE devHandle = new DEV_BROADCAST_HANDLE(bluetoothRadioHandle); RegisterDeviceNotificationSafeHandle hDevNotification = UnsafeNativeMethods.RegisterDeviceNotification_SafeHandle(windowHandle, ref devHandle, RegisterDeviceNotificationFlags.DEVICE_NOTIFY_WINDOW_HANDLE); if (hDevNotification.IsInvalid) { throw new Win32Exception(/*error code from GetLastError*/); } _hDevNotification = hDevNotification; }
internal void btRegister(IntPtr bluetoothRadioHandle) { Debug.Assert(_hDevNotification == null, "btRegister, already set."); Debug.Assert(_hDevNotification == null || _hDevNotification.IsInvalid, "btRegister, already registered."); IntPtr windowHandle = _wndProc.WindowHandle; DEV_BROADCAST_HANDLE devHandle = new DEV_BROADCAST_HANDLE(bluetoothRadioHandle); RegisterDeviceNotificationSafeHandle hDevNotification = RegisterDeviceNotification_SafeHandle(windowHandle, ref devHandle, RegisterDeviceNotificationFlags.DEVICE_NOTIFY_WINDOW_HANDLE); if (hDevNotification.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error()); } _hDevNotification = hDevNotification; }
//*********************************************************************************************** private void RegisterForDeviceChange(bool register) { if (register) { DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE(); data.dbch_devicetype = DBT_DEVTYP_HANDLE; data.dbch_reserved = 0; data.dbch_nameoffset = 0; data.dbch_hdevnotify = (IntPtr)0; int size = Marshal.SizeOf(data); data.dbch_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, buffer, true); RegisterDeviceNotification(this.Handle, buffer, 0); } else { /* * // close the directory handle * if (mDirHandle != IntPtr.Zero) * { * Native.CloseDirectoryHandle(mDirHandle); * // string er = Marshal.GetLastWin32Error().ToString(); * } * * // unregister * if (mDeviceNotifyHandle != IntPtr.Zero) * { * Native.UnregisterDeviceNotification(mDeviceNotifyHandle); * } * * * mDeviceNotifyHandle = IntPtr.Zero; * mDirHandle = IntPtr.Zero; * * mCurrentDrive = ""; * if (mFileOnFlash != null) * { * mFileOnFlash.Close(); * mFileOnFlash = null; * } */ } }
private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { // Only checking changed event since it gets called when devices are added and removed // while remove notifications don't always get called. if (msg == WmDevicechange) { if (lparam != IntPtr.Zero) { DEV_BROADCAST_HANDLE dbh = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(lparam, typeof(DEV_BROADCAST_HANDLE)); if (dbh.dbch_eventguid == GUID_BLUETOOTH_HCI_EVENT) { // BTH_HCI_EVENT_INFO OnDevicesUpdated?.Invoke(); } else if (dbh.dbch_eventguid == GUID_BLUETOOTH_L2CAP_EVENT) { // BTH_L2CAP_EVENT_INFO (not needed as controller connection triggers both HCI & L2CAP EVENTS) } } } handled = false; return(IntPtr.Zero); }
public static extern IntPtr RegisterDeviceNotification(IntPtr recipient, DEV_BROADCAST_HANDLE lpDevBroadcastHdr, RDNFlags flags);
private void DoDevTypHandle(ref Message m, ref String text) { #if DEBUG WindowsBluetoothDeviceInfo dev = null; #endif DEV_BROADCAST_HANDLE hdrHandle = (DEV_BROADCAST_HANDLE)m.GetLParam(typeof(DEV_BROADCAST_HANDLE)); var pData = Utils.Pointers.Add(m.LParam, _OffsetOfData); if (BluetoothDeviceNotificationEvent.BthPortDeviceInterface == hdrHandle.dbch_eventguid) { text += "GUID_BTHPORT_DEVICE_INTERFACE"; } else if (BluetoothDeviceNotificationEvent.RadioInRange == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_RADIO_IN_RANGE"; BTH_RADIO_IN_RANGE inRange = (BTH_RADIO_IN_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_IN_RANGE)); text += String.Format(System.Globalization.CultureInfo.InvariantCulture, " 0x{0:X12}", inRange.deviceInfo.address); text += String.Format(System.Globalization.CultureInfo.InvariantCulture, " is ({0}) 0x{0:X}", inRange.deviceInfo.flags); text += String.Format(System.Globalization.CultureInfo.InvariantCulture, " was ({0}) 0x{0:X}", inRange.previousDeviceFlags); var bdi0 = BLUETOOTH_DEVICE_INFO.Create(inRange.deviceInfo); var e = BluetoothWin32RadioInRangeEventArgs.Create( inRange.previousDeviceFlags, inRange.deviceInfo.flags, bdi0); #if DEBUG dev = new WindowsBluetoothDeviceInfo(bdi0); Debug.WriteLine("InRange: " + dev.DeviceAddress); #endif RaiseInRange(e); } else if (BluetoothDeviceNotificationEvent.RadioOutOfRange == hdrHandle.dbch_eventguid) { BTH_RADIO_OUT_OF_RANGE outOfRange = (BTH_RADIO_OUT_OF_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_OUT_OF_RANGE)); text += "GUID_BLUETOOTH_RADIO_OUT_OF_RANGE"; text += String.Format(System.Globalization.CultureInfo.InvariantCulture, " 0x{0:X12}", outOfRange.deviceAddress); var e = BluetoothWin32RadioOutOfRangeEventArgs.Create( outOfRange.deviceAddress); Debug.WriteLine("OutOfRange: " + outOfRange.deviceAddress); RaiseOutOfRange(e); } else if (BluetoothDeviceNotificationEvent.PinRequest == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_PIN_REQUEST"; // "This message should be ignored by the application. // If the application must receive PIN requests, the // BluetoothRegisterForAuthentication function should be used." } else if (BluetoothDeviceNotificationEvent.L2capEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_L2CAP_EVENT"; // struct BTH_L2CAP_EVENT_INFO { // BTH_ADDR bthAddress; USHORT psm; UCHAR connected; UCHAR initiated; } #if DEBUG var l2capE = Marshal_PtrToStructure <BTH_L2CAP_EVENT_INFO>(pData); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "L2CAP_EVENT: addr: {0:X}, psm: {1}, conn: {2}, init'd: {3}", l2capE.bthAddress, l2capE.psm, l2capE.connected, l2capE.initiated)); #endif } else if (BluetoothDeviceNotificationEvent.HciEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_HCI_EVENT"; // struct BTH_HCI_EVENT_INFO { // BTH_ADDR bthAddress; UCHAR connectionType; UCHAR connected; } #if DEBUG var hciE = Marshal_PtrToStructure <BTH_HCI_EVENT_INFO>(pData); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "HCI_EVENT: addr: {0:X}, type: {1}, conn: {2}", hciE.bthAddress, hciE.connectionType, hciE.connected)); #endif } // -- New somewhere after WinXP. else if (BluetoothDeviceNotificationEvent.AuthenticationRequestEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_AUTHENTICATION_REQUEST"; // Same content as BluetoothRegisterForAuthenticationEx } else if (BluetoothDeviceNotificationEvent.KeyPressEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_KEYPRESS_EVENT"; // struct BTH_HCI_KEYPRESS_INFO { // BTH_ADDR BTH_ADDR; UCHAR NotificationType; // HCI_KEYPRESS_XXX value } } else if (BluetoothDeviceNotificationEvent.HciVendorEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_HCI_VENDOR_EVENT"; } // -- Unknown else { text += "Unknown event: " + hdrHandle.dbch_eventguid; } Debug.WriteLine("Text: " + text); }
static public extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_HANDLE filterData, DBCH_DEVICETYPE flags);
/// <summary> /// New version which gets the handle automatically for specified directory /// Only for registering! Unregister with the old version of this function... /// </summary> /// <param name="register"></param> /// <param name="dirPath">e.g. C:\\dir</param> private void RegisterForDeviceChange(string dirPath) { IntPtr handle = Native.OpenDirectory(dirPath); if (handle == IntPtr.Zero) { mDeviceNotifyHandle = IntPtr.Zero; return; } else mDirHandle = handle; // save handle for closing it when unregistering // Register for handle DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE(); data.dbch_devicetype = DBT_DEVTYP_HANDLE; data.dbch_reserved = 0; data.dbch_nameoffset = 0; //data.dbch_data = null; //data.dbch_eventguid = 0; data.dbch_handle = handle; data.dbch_hdevnotify = (IntPtr)0; int size = Marshal.SizeOf(data); data.dbch_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, buffer, true); mDeviceNotifyHandle = Native.RegisterDeviceNotification(mRecipientHandle, buffer, 0); }
private void InitializeEvents() { if (eventHandles.Count == 0) { IntPtr hwnd = IntPtr.Zero; // Attempt to get the main window handle (HWND) of the application. while (hwnd == IntPtr.Zero) { hwnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; System.Threading.Thread.Sleep(500); } // Register for the Bluetooth events (these are sent back to the main window handle. DEV_BROADCAST_HANDLE dbh = new DEV_BROADCAST_HANDLE(); dbh.dbch_size = System.Runtime.InteropServices.Marshal.SizeOf(dbh); dbh.dbch_devicetype = DBT_DEVTYP_HANDLE; dbh.dbch_handle = InTheHand.Net.Bluetooth.BluetoothRadio.AllRadios[0].Handle; dbh.dbch_eventguid = GUID_BLUETOOTH_RADIO_IN_RANGE; this.eventHandles.Add(RegisterDeviceNotification(hwnd, ref dbh, 0)); dbh.dbch_eventguid = GUID_BLUETOOTH_RADIO_OUT_OF_RANGE; this.eventHandles.Add(RegisterDeviceNotification(hwnd, ref dbh, 0)); // WPF Interop // Adds a hook method to the main application (native) message pump System.Windows.Interop.HwndSource hwndSource = System.Windows.Interop.HwndSource.FromHwnd(hwnd); hwndSource.AddHook(new System.Windows.Interop.HwndSourceHook(HwndBluetoothHook)); } }
private static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, ref DEV_BROADCAST_HANDLE NotificationFilter, int Flags);
private void DoDevTypHandle(ref WndProcClient.WindowMessage m, ref String text) { DEV_BROADCAST_HANDLE hdrHandle = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(m.lParam, typeof(DEV_BROADCAST_HANDLE)); var pData = PointerUtils.Add(m.lParam, _OffsetOfData); if (BluetoothDeviceNotificationEvent.BthPortDeviceInterface == hdrHandle.dbch_eventguid) { text += "GUID_BTHPORT_DEVICE_INTERFACE"; } else if (BluetoothDeviceNotificationEvent.RadioInRange == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_RADIO_IN_RANGE"; BTH_RADIO_IN_RANGE inRange = (BTH_RADIO_IN_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_IN_RANGE)); text += String.Format(" 0x{0:X12}", inRange.deviceInfo.address); text += String.Format(" is ({0}) 0x{0:X}", inRange.deviceInfo.flags); text += String.Format(" was ({0}) 0x{0:X}", inRange.previousDeviceFlags); var bdi0 = BLUETOOTH_DEVICE_INFO.Create(inRange.deviceInfo); var e = BluetoothWin32RadioInRangeEventArgs.Create( inRange.previousDeviceFlags, inRange.deviceInfo.flags, bdi0); DeviceInRange?.Invoke(this, e); } else if (BluetoothDeviceNotificationEvent.RadioOutOfRange == hdrHandle.dbch_eventguid) { BTH_RADIO_OUT_OF_RANGE outOfRange = (BTH_RADIO_OUT_OF_RANGE)Marshal.PtrToStructure(pData, typeof(BTH_RADIO_OUT_OF_RANGE)); text += "GUID_BLUETOOTH_RADIO_OUT_OF_RANGE"; text += String.Format(System.Globalization.CultureInfo.InvariantCulture, " 0x{0:X12}", outOfRange.deviceAddress); var e = BluetoothWin32RadioOutOfRangeEventArgs.Create( outOfRange.deviceAddress); DeviceOutOfRange?.Invoke(this, e); } else if (BluetoothDeviceNotificationEvent.PinRequest == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_PIN_REQUEST"; } else if (BluetoothDeviceNotificationEvent.L2capEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_L2CAP_EVENT"; } else if (BluetoothDeviceNotificationEvent.HciEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_HCI_EVENT"; } else if (BluetoothDeviceNotificationEvent.AuthenticationRequestEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_AUTHENTICATION_REQUEST"; } else if (BluetoothDeviceNotificationEvent.KeyPressEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_KEYPRESS_EVENT"; } else if (BluetoothDeviceNotificationEvent.HciVendorEvent == hdrHandle.dbch_eventguid) { text += "GUID_BLUETOOTH_HCI_VENDOR_EVENT"; } else { text += "Unknown event: " + hdrHandle.dbch_eventguid; } Log.Verbose("Interop.Win32: Device changed: " + text); }
internal static extern RegisterDeviceNotificationSafeHandle RegisterDeviceNotification_SafeHandle( IntPtr hRecipient, ref DEV_BROADCAST_HANDLE notificationFilter, RegisterDeviceNotificationFlags flags );
internal static extern int RegisterDeviceNotification(IntPtr handle, ref DEV_BROADCAST_HANDLE notificationFilter, DEVICE_NOTIFY flags);
/// <summary> /// Registers to be notified when the volume is about to be removed /// This is requierd if you want to get the QUERY REMOVE messages /// </summary> /// <param name="register">true to register, false to unregister</param> /// <param name="fileHandle">handle of a file opened on the removable drive</param> private void RegisterForDeviceChange(bool register, SafeFileHandle fileHandle) { if (register) { // Register for handle DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE(); data.dbch_devicetype = DBT_DEVTYP_HANDLE; data.dbch_reserved = 0; data.dbch_nameoffset = 0; //data.dbch_data = null; //data.dbch_eventguid = 0; data.dbch_handle = fileHandle.DangerousGetHandle(); //Marshal. fileHandle; data.dbch_hdevnotify = (IntPtr)0; int size = Marshal.SizeOf(data); data.dbch_size = size; IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, buffer, true); mDeviceNotifyHandle = Native.RegisterDeviceNotification(mRecipientHandle, buffer, 0); } else { // close the directory handle if (mDirHandle != IntPtr.Zero) { Native.CloseDirectoryHandle(mDirHandle); // string er = Marshal.GetLastWin32Error().ToString(); } // unregister if (mDeviceNotifyHandle != IntPtr.Zero) { Native.UnregisterDeviceNotification(mDeviceNotifyHandle); } mDeviceNotifyHandle = IntPtr.Zero; mDirHandle = IntPtr.Zero; mCurrentDrive = ""; if (mFileOnFlash != null) { mFileOnFlash.Close(); mFileOnFlash = null; } } }
public bool WndProc(ref Message m) { bool callBaseWndProc = true; try { if (m.Msg == WM_DEVICECHANGE) { int wParam = m.WParam.ToInt32(); if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE || wParam == DBT_DEVICEQUERYREMOVE) { DEV_BROADCAST_HDR baseStructure = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); switch (baseStructure.dbch_devicetype) { case DBT_DEVTYP_VOLUME: { DEV_BROADCAST_VOLUME vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); char driveLetter = DriveMaskToLetter(vol.dbcv_unitmask); switch (wParam) { case DBT_DEVICEARRIVAL: HandleDeviceInserted(driveLetter); break; case DBT_DEVICEREMOVECOMPLETE: HandleDeviceRemoved(driveLetter); break; } } break; case DBT_DEVTYP_HANDLE: { DEV_BROADCAST_HANDLE handle = (DEV_BROADCAST_HANDLE)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); RemoveQueryHook hookedDrive = null; lock ( mEventHandlerMutex ) { hookedDrive = FindHookedDrive(handle.dbch_hdevnotify); } if (hookedDrive != null) { switch (wParam) { case DBT_DEVICEQUERYREMOVE: callBaseWndProc = HandleDeviceRemoveQuery(hookedDrive.DriveLetter, ref m); break; } } } break; } } } } catch (System.Exception e) { UsbDriveList.__LogError(this, "DriveDetector.WndProc", e); } return(callBaseWndProc); }