/// <summary> /// Message handler which must be called from client form. Processes Windows messages and calls event handlers. /// </summary> /// <param name="m"></param> public void WndProc(ref Message m) { int devType; if (m.Msg == WM_DEVICECHANGE) { var changeType = (DBT)m.WParam.ToInt32(); var volumeInfo = new DEV_BROADCAST_VOLUME(); if (changeType == DBT.DBT_DEVICEARRIVAL || changeType == DBT.DBT_DEVICEREMOVECOMPLETE) { devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { volumeInfo = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); } } var e = new DeviceDetectorEventArgs(changeType, volumeInfo); if (DeviceChanged != null) DeviceChanged(this, e); switch (changeType) { // Device is about to be removed. Any application can cancel the removal. case DBT.DBT_DEVICEQUERYREMOVE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_HANDLE) { // If the client wants to cancel, let Windows know. if (e.Cancel) m.Result = (IntPtr)BROADCAST_QUERY_DENY; } break; } } }
/// <summary> /// Message handler which must be called from client form. Processes Windows messages and calls event handlers. /// </summary> /// <param name="m"></param> public void WndProc(ref Message m) { int devType; if (m.Msg == WM_DEVICECHANGE) { var changeType = (DBT)m.WParam.ToInt32(); var volumeInfo = new DEV_BROADCAST_VOLUME(); if (changeType == DBT.DBT_DEVICEARRIVAL || changeType == DBT.DBT_DEVICEREMOVECOMPLETE) { devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { volumeInfo = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); } } var e = new DeviceDetectorEventArgs(changeType, volumeInfo); if (DeviceChanged != null) { DeviceChanged(this, e); } switch (changeType) { // Device is about to be removed. Any application can cancel the removal. case DBT.DBT_DEVICEQUERYREMOVE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_HANDLE) { // If the client wants to cancel, let Windows know. if (e.Cancel) { m.Result = (IntPtr)BROADCAST_QUERY_DENY; } } break; } } }
void _Detector_DeviceChanged(object sender, DeviceDetectorEventArgs e) { InfoLabel.Text = e.ChangeType.ToString(); }
void detector_DeviceChanged(object sender, DeviceDetectorEventArgs e) { forceRecountDevices = true; }