Ejemplo n.º 1
0
        private void OnDeviceConnected(Object sender, EventArrivedEventArgs e)
        {
            try
            {
                ManagementBaseObject targetInstance = e.NewEvent.GetPropertyValue("TargetInstance") as ManagementBaseObject;

                Win32UsbControllerDevice win32UsbControllerDevice = this.GetDevice(targetInstance);

                if (null == win32UsbControllerDevice)
                {
                    return;
                }

                if (this.DeviceConnected != null)
                {
                    this.DeviceConnected(this, new Win32UsbControllerDeviceEventArgs(win32UsbControllerDevice));
                }
            }
            catch (Exception ex)
            {
                Tracer.Trace(ex, "Error handling WMI creation event");
            }
        }
 public Win32UsbControllerDeviceEventArgs(Win32UsbControllerDevice win32UsbControllerDevice)
 {
     this.Device = win32UsbControllerDevice;
 }
Ejemplo n.º 3
0
        private Win32UsbControllerDevice GetDevice(ManagementBaseObject managementObject)
        {
            try
            {
                Win32UsbControllerDevice win32UsbControllerDevice = new Win32UsbControllerDevice();

                try
                {
                    String dependent = managementObject.GetPropertyValue("Dependent").ToString();
                    win32UsbControllerDevice.DeviceId = this.ExtractSeviceId(dependent);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    String antecedent = managementObject.GetPropertyValue("Antecedent").ToString();
                    win32UsbControllerDevice.ControllerId = this.ExtractSeviceId(antecedent);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    String locationInformation = this.GetLocationInformation(win32UsbControllerDevice.DeviceId);

                    Int32 hubIndex  = locationInformation.IndexOf("HUB_#", StringComparison.OrdinalIgnoreCase);
                    Int32 portIndex = locationInformation.IndexOf("PORT_#", StringComparison.OrdinalIgnoreCase);

                    if ((hubIndex < 0) || (portIndex < 0))
                    {
                        throw new Exception("Wrong location information format");
                    }

                    win32UsbControllerDevice.Hub  = locationInformation.Substring(hubIndex + 5, 4);
                    win32UsbControllerDevice.Port = locationInformation.Substring(portIndex + 6, 4);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    Int32 vidIndex = win32UsbControllerDevice.DeviceId.IndexOf("VID", StringComparison.OrdinalIgnoreCase);
                    Int32 pidIndex = win32UsbControllerDevice.DeviceId.IndexOf("PID", StringComparison.OrdinalIgnoreCase);

                    if ((vidIndex < 0) || (pidIndex < 0))
                    {
                        throw new Exception("Wrong device ID format");
                    }

                    win32UsbControllerDevice.Vid = win32UsbControllerDevice.DeviceId.Substring(vidIndex + 4, 4);
                    win32UsbControllerDevice.Pid = win32UsbControllerDevice.DeviceId.Substring(pidIndex + 4, 4);
                }
                catch (Exception ex)
                {
                }

                return(win32UsbControllerDevice);
            }
            catch (Exception ex)
            {
            }

            return(null);
        }