/// <summary> /// Wait for windows to install drivers for new device and when device become available. /// </summary> private void StartCheckingIfInstalled() { while (_requestStop == false) { IsInstalled = _WMIQuery.GetStatusLevel(PNPID); if (IsInstalled || PNPID == null) { RequestStop(); } else { //Waiting for device to be installed } Thread.Sleep(1000); } if (IsInstalled == true) { //Device has been installed UpdateSetting(); if (ApplyPatch) { //apply patch to Realtek device, we do not care if it exists or not at this point InstallFromCD rtl = new InstallFromCD(); rtl.InstallRealtekPatch(); ApplyPatch = false; } } }
/// <summary> /// Get current configuration settings from registry /// </summary> private void GetCurrentSetting() { UseHardware = _RegConfig.GetRegUseHardwareID(); //check exisitng devices List <string> pnpIDList = new List <string>(); if (UseHardware) { HardwareIDs = _RegConfig.GetRegHardwareIDs(); //add Realtek USB CD-ROM as we must monitor for this as well HardwareIDs.Add(RLTCDROM_HID); foreach (var hID in HardwareIDs) { //Checking for HardwareID List <string> tempList = new List <string>(_WMIQuery.GetPNDeviceID(hID, true)); foreach (var item in tempList) { pnpIDList.Add(item); } } } else { Descriptions = _RegConfig.GetRegDeviceDescriptions(); foreach (var info in Descriptions) { //checking for Description List <string> tempList = new List <string>(_WMIQuery.GetPNDeviceID(info)); foreach (var item in tempList) { pnpIDList.Add(item); } } } //check if list is not empty bool isEmpty = ListHelper.IsNullOrEmpty(pnpIDList); if (!isEmpty) { //get first item in the list only List <string> distinctPNPID = pnpIDList.Distinct().ToList(); DevicePnpIDList = distinctPNPID; //GetCurrentSetting: Found current device ID CurrentDeviceID = DevicePnpIDList.FirstOrDefault(); if (CurrentDeviceID.Contains(RealTekCD_PNP)) { //Found Realtek Flush Drive //Install current driver from RealTek Flush Drive as the network adapter has change to be CD-ROM InstallFromCD drive = new InstallFromCD(); drive.GetRealtekCDROM(); _NetMonitor.ApplyPatch = true; } _NetMonitor.PNPID = CurrentDeviceID; } else { //nNo device(s) found } }
//Event watcher messages arrived void eventWatcher_EventArrived(object sender, EventArrivedEventArgs e) { if (UseHardware) { ManagementObjectSearcher search = new ManagementObjectSearcher("root\\CIMV2", "SELECT HardwareID, PNPDeviceID FROM Win32_PnPEntity"); EventArrived(search, UseHardware); } else { ManagementObjectSearcher search = new ManagementObjectSearcher("root\\CIMV2", "SELECT Description, PNPDeviceID FROM Win32_PnPEntity"); EventArrived(search); } bool USBConnected = true; ManagementBaseObject baseObject = (ManagementBaseObject)e.NewEvent; if (baseObject.ClassPath.ClassName.Equals("__InstanceCreationEvent")) { //Console.WriteLine("Connected"); //Device Connected USBConnected = true; List <string> distinctItem = DevicePnpIDList.Distinct().ToList(); DevicePnpIDList = distinctItem; if (DevicePnpIDList.Count > 0) { for (int i = 0; i < 1; i++) { CurrentDeviceID = DevicePnpIDList[i]; } } } else if (baseObject.ClassPath.ClassName.Equals("__InstanceDeletionEvent")) { //Device Removed for (int i = DevicePnpIDList.Count - 1; i >= 0; i--) { var connected = _WMIQuery.IsConnected(DevicePnpIDList[i]); if (!connected) { DevicePnpIDList.RemoveAt(i); } } if (DevicePnpIDList.Count > 0) { for (int i = 0; i < 1; i++) { CurrentDeviceID = DevicePnpIDList[i]; } } else { CurrentDeviceID = null; USBConnected = false; } } if (CurrentDeviceID.Contains(RealTekCD_PNP)) { //Found Realtek Flush Drive //Install current driver from RealTek Flush Drive as the network adapter has change to be CD-ROM InstallFromCD drive = new InstallFromCD(); drive.GetRealtekCDROM(); _NetMonitor.ApplyPatch = true; } _NetMonitor.PNPID = CurrentDeviceID; }