/// <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 = (WM_DEVICECHANGE)m.WParam.ToInt32(); var volumeInfo = new DEV_BROADCAST_VOLUME(); if (changeType == Win32.WM_DEVICECHANGE.DBT_DEVICEARRIVAL || changeType == Win32.WM_DEVICECHANGE.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 Win32.WM_DEVICECHANGE.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; } } }
public DeviceDetectorEventArgs(WM_DEVICECHANGE changeType, DEV_BROADCAST_VOLUME volumeInfo) { Cancel = false; _ChangeType = changeType; _VolumeInfo = volumeInfo; }