private void Notify(DriveDetectorEventHandler handler, WindowMessage msg) { if (handler == null) { return; } var vol = msg.GetLParamAsStruct <DEV_BROADCAST_VOLUME>(); var driveLetter = DriveMaskToLetter(vol.dbcv_unitmask); var drivePath = driveLetter + @":\"; var args = new DriveDetectorEventArgs { DriveInfo = DriveInfo.GetDrives().FirstOrDefault(info => info.Name == drivePath) }; handler(this, args); }
private void Notify(DriveDetectorEventHandler handler, Message m) { if (handler == null) { return; } var vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); var driveLetter = DriveMaskToLetter(vol.dbcv_unitmask); var drivePath = driveLetter + @":\"; var args = new DriveDetectorEventArgs { DriveInfo = DriveInfo.GetDrives().FirstOrDefault(info => info.Name == drivePath) }; handler(this, args); }
internal void WndProc(ref Message m) { if (m.Msg == NativeInterop.WM_DEVICECHANGE) { var wParam = m.WParam.ToInt32(); switch (wParam) { case NativeInterop.DBT_DEVICEARRIVAL: case NativeInterop.DBT_DEVICEREMOVECOMPLETE: { var devType = Marshal.ReadInt32(m.LParam, 4); if (devType == NativeInterop.DBT_DEVTYP_VOLUME) { var vol = (NativeInterop.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(NativeInterop.DEV_BROADCAST_VOLUME)); var driveLetter = DriveMaskToLetter(vol.dbcv_unitmask); DriveDetectorEventHandler handler = null; if (wParam == NativeInterop.DBT_DEVICEARRIVAL) { handler = DeviceArrived; } else { handler = DeviceRemoved; } if (handler != null) { var args = new DriveDetectorEventArgs($"{driveLetter}:\\"); handler(this, args); } } break; } } } }
public static bool CreateDriveDetector( DriveDetectorEventHandler driveArrived, DriveDetectorEventHandler driveRemoved, Form fm, out DriveDetector detector) { detector = null; if (driveArrived == null || driveRemoved == null || fm == null) { return(false); } try { detector = new DriveDetector(fm); detector.DeviceArrived += driveArrived; detector.DeviceRemoved += driveRemoved; return(true); } catch { return(false); } }
/// <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; char c; if (m.Msg == W_DEVICECHANGE) { // W_DEVICECHANGE can have several meanings depending on the WParam value... switch (m.WParam.ToInt32()) { // // New device has just arrived // case DBT_DEVICEARRIVAL: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); // Get the drive letter c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // // We should create copy of the event before testing it and // calling the delegate - if any DriveDetectorEventHandler tempDeviceArrived = DeviceArrived; if (tempDeviceArrived != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceArrived(this, e); // Register for query remove if requested if (e.HookQueryRemove) { // If something is already hooked, unhook it now if (mDeviceNotifyHandle != IntPtr.Zero) { RegisterForDeviceChange(false, null); } RegisterQuery(c + ":\\"); } } // if has event handler } break; // // Device is about to be removed // Any application can cancel the removal // case DBT_DEVICEQUERYREMOVE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_HANDLE) { // TODO: we could get the handle for which this message is sent // from vol.dbch_handle and compare it agains a list of handles for // which we have registered the query remove message (?) //DEV_BROADCAST_HANDLE vol; //vol = (DEV_BROADCAST_HANDLE) // Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); // if ( vol.dbch_handle .... // // Call the event handler in client // DriveDetectorEventHandler tempQuery = QueryRemove; if (tempQuery != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = mCurrentDrive; // drive which is hooked tempQuery(this, e); // If the client wants to cancel, let Windows know if (e.Cancel) { m.Result = (IntPtr)BROADCAST_QUERY_DENY; } else { // Close the handle so that the drive can be unmounted if (mFileOnFlash != null) { mFileOnFlash.Close(); mFileOnFlash = null; } } } } break; // // Device has been removed // case DBT_DEVICEREMOVECOMPLETE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved; if (tempDeviceRemoved != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceRemoved(this, e); } // TODO: we could unregister the notify handle here if we knew it is the // right drive which has been just removed //RegisterForDeviceChange(false, null); } } 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; char c; if (m.Msg == WM_DEVICECHANGE) { // WM_DEVICECHANGE can have several meanings depending on the WParam value... switch (m.WParam.ToInt32()) { // // New device has just arrived // case DBT_DEVICEARRIVAL: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); // Get the drive letter c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // // We should create copy of the event before testing it and // calling the delegate - if any DriveDetectorEventHandler tempDeviceArrived = DeviceArrived; if (tempDeviceArrived != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceArrived(this, e); // Register for query remove if requested if (e.HookQueryRemove) { // If something is already hooked, unhook it now if (mDeviceNotifyHandle != IntPtr.Zero) { RegisterForDeviceChange(false, null); } RegisterQuery(c + ":\\"); } } // if has event handler } break; // // Device is about to be removed // Any application can cancel the removal // case DBT_DEVICEQUERYREMOVE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_HANDLE) { // TODO: we could get the handle for which this message is sent // from vol.dbch_handle and compare it against a list of handles for // which we have registered the query remove message (?) //DEV_BROADCAST_HANDLE vol; //vol = (DEV_BROADCAST_HANDLE) // Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); // if ( vol.dbch_handle .... // // Call the event handler in client // DriveDetectorEventHandler tempQuery = QueryRemove; if (tempQuery != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = mCurrentDrive; // drive which is hooked tempQuery(this, e); // If the client wants to cancel, let Windows know if (e.Cancel) { m.Result = (IntPtr)BROADCAST_QUERY_DENY; } else { // Change 28.10.2007: Unregister the notification, this will // close the handle to file or root directory also. // We have to close it anyway to allow the removal so // even if some other app cancels the removal we would not know about it... RegisterForDeviceChange(false, null); // will also close the mFileOnFlash } } } break; // // Device has been removed // case DBT_DEVICEREMOVECOMPLETE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved; if (tempDeviceRemoved != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceRemoved(this, e); } // TODO: we could unregister the notify handle here if we knew it is the // right drive which has been just removed //RegisterForDeviceChange(false, null); } } break; //hatton added // we can't tell much at this point, but other state-logic may be able to use // this indicator to figure out what did. I'm using it to detect possible // *physical removal of the usb key* after we've "ejected" it (unmounted, more acurately) case DBT_DEVNODES_CHANGED: if (DeviceSomethingHappened != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = string.Empty; //we don't know DeviceSomethingHappened(this, e); } break; } } }
private void Notify(DriveDetectorEventHandler handler, Message m) { if (handler == null) return; var vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); var driveLetter = DriveMaskToLetter(vol.dbcv_unitmask); var drivePath = driveLetter + @":\"; var args = new DriveDetectorEventArgs { DriveInfo = DriveInfo.GetDrives().FirstOrDefault(info => info.Name == drivePath) }; handler(this, args); }
/// <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; char c; if (m.Msg == WM_DEVICECHANGE) { // WM_DEVICECHANGE can have several meanings depending on the WParam value... switch (m.WParam.ToInt32()) { // // New device has just arrived // case DBT_DEVICEARRIVAL: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_PORT) { // // Call the client event handler // // We should create copy of the event before testing it and // calling the delegate - if any DriveDetectorEventHandler tempDeviceArrived = DeviceArrived; if (tempDeviceArrived != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); tempDeviceArrived(this, e); } } /* * if (devType == DBT_DEVTYP_VOLUME) * { * DEV_BROADCAST_VOLUME vol; * vol = (DEV_BROADCAST_VOLUME) * Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); * * // Get the drive letter * c = DriveMaskToLetter(vol.dbcv_unitmask); * * * // * // Call the client event handler * // * // We should create copy of the event before testing it and * // calling the delegate - if any * DriveDetectorEventHandler tempDeviceArrived = DeviceArrived; * if ( tempDeviceArrived != null ) * { * DriveDetectorEventArgs e = new DriveDetectorEventArgs(); * e.Drive = c + ":\\"; * tempDeviceArrived(this, e); * * // Register for query remove if requested * if (e.HookQueryRemove) * { * // If something is already hooked, unhook it now * if (mDeviceNotifyHandle != IntPtr.Zero) * { * RegisterForDeviceChange(false, null); * } * * RegisterQuery(c + ":\\"); * } * } // if has event handler * }*/ break; // // Device is about to be removed // Any application can cancel the removal // case DBT_DEVICEQUERYREMOVE: /* * devType = Marshal.ReadInt32(m.LParam, 4); * if (devType == DBT_DEVTYP_HANDLE) * { * // TODO: we could get the handle for which this message is sent * // from vol.dbch_handle and compare it against a list of handles for * // which we have registered the query remove message (?) * //DEV_BROADCAST_HANDLE vol; * //vol = (DEV_BROADCAST_HANDLE) * // Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); * // if ( vol.dbch_handle .... * * * // * // Call the event handler in client * // * DriveDetectorEventHandler tempQuery = QueryRemove; * if (tempQuery != null) * { * DriveDetectorEventArgs e = new DriveDetectorEventArgs(); * e.Drive = mCurrentDrive; // drive which is hooked * tempQuery(this, e); * * // If the client wants to cancel, let Windows know * if (e.Cancel) * { * m.Result = (IntPtr)BROADCAST_QUERY_DENY; * } * else * { * // Change 28.10.2007: Unregister the notification, this will * // close the handle to file or root directory also. * // We have to close it anyway to allow the removal so * // even if some other app cancels the removal we would not know about it... * RegisterForDeviceChange(false, null); // will also close the mFileOnFlash * } * * } * }*/ break; // // Device has been removed // case DBT_DEVICEREMOVECOMPLETE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_PORT) { // // Call the client event handler // DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved; if (tempDeviceRemoved != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); tempDeviceRemoved(this, e); } } /* * if (devType == DBT_DEVTYP_VOLUME) * { * devType = Marshal.ReadInt32(m.LParam, 4); * if (devType == DBT_DEVTYP_VOLUME) * { * DEV_BROADCAST_VOLUME vol; * vol = (DEV_BROADCAST_VOLUME) * Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); * c = DriveMaskToLetter(vol.dbcv_unitmask); * * // * // Call the client event handler * // * DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved; * if (tempDeviceRemoved != null) * { * DriveDetectorEventArgs e = new DriveDetectorEventArgs(); * e.Drive = c + ":\\"; * tempDeviceRemoved(this, e); * } * * // TODO: we could unregister the notify handle here if we knew it is the * // right drive which has been just removed * //RegisterForDeviceChange(false, null); * } * }*/ break; } } }
private const int DBT_DEVTYP_PORT = 0x00000003; // serial, parallel #region WindowProc /// <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; char c; if (m.Msg == WM_DEVICECHANGE) { //devType = Marshal.ReadInt32(m.LParam, 4); //if (devType == DBT_DEVTYP_PORT) //{ // usb device inserted, call the query mDeleg = new getFriendlyNameListDelegate(getFriendlyNameList); AsyncCallback callback = new AsyncCallback(getFriendlyNameListCallback); // invoke the thread that will handle getting the friendly names mDeleg.BeginInvoke(callback, new object()); //} // WM_DEVICECHANGE can have several meanings depending on the WParam value... switch (m.WParam.ToInt32()) { case DBT_IPHONE_HANDLE: IPhoneAttached(this, new DriveDetectorEventArgs()); break; // // New device has just arrived // case DBT_DEVICEARRIVAL: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); // Get the drive letter c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // // We should create copy of the event before testing it and // calling the delegate - if any DriveDetectorEventHandler tempDeviceArrived = DeviceArrived; if (tempDeviceArrived != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceArrived(this, e); // Register for query remove if requested if (e.HookQueryRemove) { // If something is already hooked, unhook it now if (mDeviceNotifyHandle != IntPtr.Zero) { RegisterForDeviceChange(false, null); } RegisterQuery(c + ":\\"); } } // if has event handler } break; // // Device is about to be removed // Any application can cancel the removal // case DBT_DEVICEQUERYREMOVE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_HANDLE) { // TODO: we could get the handle for which this message is sent // from vol.dbch_handle and compare it against a list of handles for // which we have registered the query remove message (?) //DEV_BROADCAST_HANDLE vol; //vol = (DEV_BROADCAST_HANDLE) // Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HANDLE)); // if ( vol.dbch_handle .... // // Call the event handler in client // DriveDetectorEventHandler tempQuery = QueryRemove; if (tempQuery != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = mCurrentDrive; // drive which is hooked tempQuery(this, e); // If the client wants to cancel, let Windows know if (e.Cancel) { m.Result = (IntPtr)BROADCAST_QUERY_DENY; } else { // Change 28.10.2007: Unregister the notification, this will // close the handle to file or root directory also. // We have to close it anyway to allow the removal so // even if some other app cancels the removal we would not know about it... RegisterForDeviceChange(false, null); // will also close the mFileOnFlash } } } break; // // Device has been removed // case DBT_DEVICEREMOVECOMPLETE: devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { devType = Marshal.ReadInt32(m.LParam, 4); if (devType == DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME vol; vol = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); c = DriveMaskToLetter(vol.dbcv_unitmask); // // Call the client event handler // DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved; if (tempDeviceRemoved != null) { DriveDetectorEventArgs e = new DriveDetectorEventArgs(); e.Drive = c + ":\\"; tempDeviceRemoved(this, e); } // TODO: we could unregister the notify handle here if we knew it is the // right drive which has been just removed //RegisterForDeviceChange(false, null); } } break; } } }