Ejemplo n.º 1
0
        private void StateChange(int newState, int selectedItem, IntPtr hDevInfo)
        {
            var propChangeParams = new UsbApi.SpPropchangeParams();

            propChangeParams.Init();
            var deviceInfoData = new UsbApi.SpDevinfoData();

            propChangeParams.ClassInstallHeader.CbSize = Marshal.SizeOf(propChangeParams.ClassInstallHeader);
            deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData);

            if (!UsbApi.SetupDiEnumDeviceInfo(hDevInfo, selectedItem, ref deviceInfoData))
            {
                return;
            }

            propChangeParams.ClassInstallHeader.InstallFunction = UsbApi.DifPropertychange;
            propChangeParams.Scope       = UsbApi.DicsFlagGlobal;
            propChangeParams.StateChange = newState;

            if (!UsbApi.SetupDiSetClassInstallParams(hDevInfo, ref deviceInfoData, ref propChangeParams.ClassInstallHeader, Marshal.SizeOf(propChangeParams)))
            {
                return;
            }

            UsbApi.SetupDiCallClassInstaller(UsbApi.DifPropertychange, hDevInfo, ref deviceInfoData);
        }
Ejemplo n.º 2
0
            public override IoStatus Execute(DeviceCommand device)
            {
                IoStatus status = new IoStatus();

                // Get all host controllers active on the machine
                List <USBView.USBController> hostList = USBView.GetUSBControllers();

                List <string> devPaths = UsbApi.GetDevices(null, UsbApi.GUID_CLASS_USB_HOST_CONTROLLER);

                string result  = "<ul class=\"mktree\" id=\"USBView\">";
                string imgPath = ResourcePath + "\\";

                foreach (USBView.USBController hostC in hostList)
                {
                    result += "<li><img src=\"" + imgPath + "controller.ico\"/>"
                              + hostC.ControllerName + "<ul>";

                    USBView.USBController.USBHub rootHub = hostC.RootHub;
                    result += "<li><img src=\"" + imgPath + "hub.ico\"/>" + rootHub.HubName + "</li><ul>";
                    List <USBView.USBController.USBHub.USBPort> portList = rootHub.Ports;
                    foreach (USBView.USBController.USBHub.USBPort port in portList)
                    {
                        result += port.HasAttachedDevice?
                                  "<li><img src=\"" + imgPath + "controller.ico\"/>" : "<li><img src=\"" + imgPath + "port.ico\"/>";
                        result += port.PortInformation;
                        List <USBView.USBController.USBHub.USBPort.Property> properties = port.Properties;
                        if (properties.Count > 0)
                        {
                            result += "<ul>";
                            foreach (USBView.USBController.USBHub.USBPort.Property prop in properties)
                            {
                                result += "<li><b>" + prop.Key + "</b>" + prop.Value + "</li>";
                            }
                            result += "</ul>";
                        }
                        result += "</li>";
                    }
                    result += "</ul></ul></li>";
                }
                result       += "</ul>";
                status.buffer = Encoding.ASCII.GetBytes(result.ToCharArray());
                status.size   = (uint)status.buffer.Length;
                status.error  = USBError.SUCCESS;
                return(status);
            }
Ejemplo n.º 3
0
        private bool SetDevice(DeviceControlFlags deviceControlFlag, ushort vendorid, ushort productid)
        {
            Guid myGuid         = Guid.Empty;
            var  deviceInfoData = new UsbApi.SpDevinfoData1
            {
                CbSize    = 28,
                DevInst   = 0,
                ClassGuid = Guid.Empty,
                Reserved  = 0
            };
            UInt32        i          = 0;
            StringBuilder deviceName = new StringBuilder()
            {
                Capacity = UsbApi.MaxBufferSize
            };

            //The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device information elements for a local machine.
            IntPtr theDevInfo = UsbApi.SetupDiGetClassDevs(ref myGuid, 0, 0, UsbApi.DigcfAllclasses | UsbApi.DigcfPresent | UsbApi.DigcfProfile);

            for (; UsbApi.SetupDiEnumDeviceInfo(theDevInfo, i, deviceInfoData);)
            {
                if (UsbApi.SetupDiGetDeviceRegistryProperty(theDevInfo, deviceInfoData, (uint)UsbApi.Spdrp.SpdrpHardwareid, 0, deviceName, UsbApi.MaxBufferSize, IntPtr.Zero))
                {
                    if (deviceName.ToString().Contains(@"USB\Vid_") && deviceName.ToString().Contains(vendorid.ToString("x")))
                    {
                        StateChange(
                            deviceControlFlag == DeviceControlFlags.Disable ? UsbApi.DicsDisable : UsbApi.DicsEnable,
                            (int)i, theDevInfo);

                        UsbApi.SetupDiDestroyDeviceInfoList(theDevInfo);
                        break;
                    }
                }
                i++;
            }

            return(true);
        }
Ejemplo n.º 4
0
        private void Initalise(uint index)
        {
            IntPtr ptr = IntPtr.Zero;
            IntPtr deviceInfoHandle = IntPtr.Zero;

            try
            {
                ptr = Marshal.AllocHGlobal(UsbApi.MaxBufferSize);
                deviceInfoHandle = UsbApi.SetupDiGetClassDevs(ref _guid, 0, IntPtr.Zero,
                                                              UsbApi.DigcfPresent | UsbApi.DigcfDeviceinterface);

                // Create a device interface data structure
                UsbApi.SpDeviceInterfaceData deviceInterfaceData = new UsbApi.SpDeviceInterfaceData();
                deviceInterfaceData.CbSize = Marshal.SizeOf(deviceInterfaceData);

                // Start the enumeration.
                Boolean success =
                    UsbApi.SetupDiEnumDeviceInterfaces(deviceInfoHandle, IntPtr.Zero, ref _guid, (int)index,
                                                       ref deviceInterfaceData);
                if (success)
                {
                    // Build a DevInfo data structure.
                    UsbApi.SpDevinfoData deviceInfoData = new UsbApi.SpDevinfoData();
                    deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData);

                    // Build a device interface detail data structure.
                    UsbApi.SpDeviceInterfaceDetailData deviceInterfaceDetailData =
                        new UsbApi.SpDeviceInterfaceDetailData
                    {
                        CbSize = UIntPtr.Size == 8 ? 8 : (int)(4 + (uint)Marshal.SystemDefaultCharSize)
                    };

                    // Now we can get some more detailed informations.
                    int nRequiredSize = 0;
                    int nBytes        = UsbApi.MaxBufferSize;

                    if (UsbApi.SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, ref deviceInterfaceData,
                                                               ref deviceInterfaceDetailData, nBytes, ref nRequiredSize, ref deviceInfoData))
                    {
                        DevicePath = deviceInterfaceDetailData.DevicePath;

                        ParseDevicePath(DevicePath);

                        // Get the device description and driver key name.
                        int requiredSize = 0;
                        int regType      = UsbApi.RegSz;

                        if (UsbApi.SetupDiGetDeviceRegistryProperty(deviceInfoHandle, ref deviceInfoData,
                                                                    (int)UsbApi.Spdrp.SpdrpDevicedesc,
                                                                    ref regType,
                                                                    ptr,
                                                                    UsbApi.MaxBufferSize, ref requiredSize))
                        {
                            DeviceDescription = Marshal.PtrToStringAuto(ptr);
                        }

                        if (UsbApi.SetupDiGetDeviceRegistryProperty(
                                deviceInfoHandle,
                                ref deviceInfoData,
                                (int)UsbApi.Spdrp.SpdrpDriver,
                                ref regType,
                                ptr,
                                UsbApi.MaxBufferSize,
                                ref requiredSize))
                        {
                            DriverKey = Marshal.PtrToStringAuto(ptr);
                        }
                    }

                    IntPtr hostControllerHandle = IntPtr.Zero;

                    try
                    {
                        hostControllerHandle = KernelApi.CreateFile(deviceInterfaceDetailData.DevicePath,
                                                                    UsbApi.GenericWrite,
                                                                    UsbApi.FileShareWrite, IntPtr.Zero,
                                                                    UsbApi.OpenExisting, 0, IntPtr.Zero);

                        if (deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue)
                        {
                            GetHostControllerPowerMap(hostControllerHandle);

                            GetHostControllerInfo(hostControllerHandle);
                        }
                    }
                    finally
                    {
                        if (hostControllerHandle != IntPtr.Zero ||
                            deviceInfoHandle.ToInt64() != UsbApi.InvalidHandleValue)
                        {
                            KernelApi.CloseHandle(hostControllerHandle);
                        }
                    }

                    try
                    {
                        Devices.Add(new UsbHub(null, DevicePath));
                    }
                    catch (Exception ex)
                    {
                        CoreTraceSource.Source.TraceEvent(TraceEventType.Error, CoreTraceSource.UsbControllerSourceId,
                                                          "Unhandled exception occurred: {0}", ex);

                        throw new UsbControllerException("Unhandled exception occurred", ex);
                    }
                }
                else
                {
                    throw new UsbControllerException("No usb controller found!");
                }
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }

                if (deviceInfoHandle != IntPtr.Zero)
                {
                    UsbApi.SetupDiDestroyDeviceInfoList(deviceInfoHandle);
                }
            }
        }
Ejemplo n.º 5
0
        private void GetComPort()
        {
            IntPtr regValue = IntPtr.Zero;
            IntPtr regKey   = IntPtr.Zero;
            IntPtr handle   = IntPtr.Zero;
            IntPtr ptr      = IntPtr.Zero;

            try
            {
                handle =
                    UsbApi.SetupDiGetClassDevs(0, UsbApi.RegstrKeyUsb, IntPtr.Zero, UsbApi.DigcfPresent | UsbApi.DigcfAllclasses);

                if (handle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    ptr = Marshal.AllocHGlobal(UsbApi.MaxBufferSize);
                    var success = true;

                    for (var i = 0; success; i++)
                    {
                        var deviceInfoData = new UsbApi.SpDevinfoData();
                        deviceInfoData.CbSize = Marshal.SizeOf(deviceInfoData);

                        success = UsbApi.SetupDiEnumDeviceInfo(handle, i, ref deviceInfoData);

                        if (success)
                        {
                            var requiredSize = -1;
                            var regType      = UsbApi.RegSz;
                            var driverKey    = string.Empty;

                            if (UsbApi.SetupDiGetDeviceRegistryProperty(handle, ref deviceInfoData,
                                                                        (int)UsbApi.Spdrp.SpdrpDriver,
                                                                        ref regType, ptr, UsbApi.MaxBufferSize, ref requiredSize))
                            {
                                driverKey = Marshal.PtrToStringAuto(ptr);
                            }

                            if (DriverKey == driverKey)
                            {
                                regKey = UsbApi.SetupDiOpenDevRegKey(handle, ref deviceInfoData, UsbApi.DicsFlag.Global, 0, UsbApi.DiReg.Dev, (uint)WinNtApi.KeyRead);

                                int size = 0;
                                Advapi32.RegValueKind kind = Advapi32.RegValueKind.None;

                                // Get the size of buffer we will need
                                uint retVal = Advapi32.RegQueryValueEx(regKey, "PortName", 0, ref kind, IntPtr.Zero, ref size);
                                if (size == 0)
                                {
                                    break;
                                }

                                regValue = Marshal.AllocHGlobal(size);

                                if (Advapi32.RegQueryValueEx(regKey, "PortName", 0, ref kind, regValue, ref size) == 0)
                                {
                                    if (kind == Advapi32.RegValueKind.Sz)
                                    {
                                        _comPort = Marshal.PtrToStringAnsi(regValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (regValue != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(regValue);
                }

                if (regKey != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(regKey);
                }

                if (handle != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }

                if (handle != IntPtr.Zero)
                {
                    UsbApi.SetupDiDestroyDeviceInfoList(handle);
                }
            }
        }