private bool HandleDeviceRemoveQuery(char driveLetter, ref Message m) { bool callBaseWndProc = true; try { if (QueryRemove != null) { bool allowRemoval = true; if (QueryRemove != null) { QueryRemove(this, driveLetter, out allowRemoval); } if (!allowRemoval) { callBaseWndProc = false; m.Result = (IntPtr)BROADCAST_QUERY_DENY; } } } catch (System.Exception e) { UsbDriveList.__LogError(this, "DriveDetector.HandleDeviceRemoveQueryd", e); } return(callBaseWndProc); }
public static UsbDriveInfo LoadUsbFromDriveLetter(char driveLetter) { const int maxAttempts = 5; try { if (char.IsLetter(driveLetter)) { for (int attempts = 0; attempts < maxAttempts; ++attempts) { try { ManagementObject logicalObj = GetLogicalObjectFromDriveLetter(driveLetter); logicalObj.Get(); ManagementObject diskObj = GetDiskObjectFromLogical(logicalObj); string volumeName = logicalObj["VolumeName"].ToString(); object serialNumberObject = logicalObj["VolumeSerialNumber"]; string modelName = diskObj["Model"].ToString(); object pnpDeviceId = diskObj["PNPDeviceID"]; string uniqueId = FormatDriveUniqueID(serialNumberObject != null ? serialNumberObject.ToString() : "", pnpDeviceId != null ? pnpDeviceId.ToString() : ""); UsbDriveInfo driveInfo = UsbDriveList.Instance.GetUsbInfoByUniqueId(uniqueId); if (driveInfo == null) { driveInfo = UsbDriveList.Instance.CreateEmptyUsbInfo(modelName, volumeName, uniqueId); } string sizeString = logicalObj["Size"].ToString(); System.UInt64 sizeBytes = 0; System.UInt64.TryParse(sizeString, out sizeBytes); driveInfo.__MarkActive(driveLetter, sizeBytes); diskObj.Dispose(); logicalObj.Dispose(); return(driveInfo); } catch (System.Exception) { // failed, try again until maxAttempts //UsbDriveList.__LogError( null, "LoadUsbFromDriveLetter attempt failed : " + (attempts + 1) + "/" + maxAttempts, e ); System.Threading.Thread.Sleep(200); } } } else { throw new Exception("Drive letter is not valid " + driveLetter.ToString()); } } catch (System.Exception e) { UsbDriveList.__LogError(null, "UsbPrivate.LoadUsbFromDriveLetter", e); return(null); } return(null); }
public static void CreateInstance(Control control) { System.Diagnostics.Debug.Assert(mInstance == null); mInstance = new UsbDriveList(control); mInstance.Init(); if (OnCreate != null) { OnCreate(mInstance, null); } }
private void HandleDeviceRemoved(char driveLetter) { try { lock ( mEventHandlerMutex ) { mEventQueue.Enqueue(new EventObject(EventObject.EventType.Remove, driveLetter)); } } catch (System.Exception e) { UsbDriveList.__LogError(this, "DriveDetector.HandleDeviceRemoved", e); } }
public void Dispose() { try { if (mRegisterDeviceHandle != IntPtr.Zero) { Native.UnregisterDeviceNotification(mRegisterDeviceHandle); } mRegisterDeviceHandle = IntPtr.Zero; } catch (System.Exception e) { UsbDriveList.__LogError(this, "HookedDrive.Dispose failed", e); } }
void OnEventHandlerThreadStart() { while (mThreadRunning) { try { lock ( mEventHandlerMutex ) { while (mEventQueue.Count > 0) { EventObject eventObject = mEventQueue.Dequeue(); switch (eventObject.Event) { case EventObject.EventType.Insert: { HookQueryRemove(eventObject.DriveLetter); if (DeviceInserted != null) { if (DeviceInserted != null) { DeviceInserted(this, eventObject.DriveLetter); } } } break; case EventObject.EventType.Remove: { RemoveHookedDrive(FindHookedDrive(eventObject.DriveLetter)); if (DeviceRemoved != null) { DeviceRemoved(this, eventObject.DriveLetter); } } break; } } } } catch (System.Exception e) { UsbDriveList.__LogError(this, "OnEventHandlerThreadStart", e); } System.Threading.Thread.Sleep(500); } }
public void Dispose() { try { mThreadRunning = false; lock ( mEventHandlerMutex ) { foreach (RemoveQueryHook drive in mHookedDrives) { drive.Dispose(); } mHookedDrives.Clear(); } } catch (System.Exception e) { UsbDriveList.__LogError(this, "DriveDetector.Dispose", e); } }
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); }