Пример #1
0
        private static String GetDeviceRegistryProperty(IntPtr hDevInfo,
                                                        ref SetupApi.SP_DEVINFO_DATA deviceInfoData,
                                                        UInt32 property)
        {
            IntPtr        dataType = IntPtr.Zero;
            uint          size;
            StringBuilder buffer;

            SetupApi.SetupDiGetDeviceRegistryProperty0(
                hDevInfo,
                ref deviceInfoData,
                property,
                out dataType,
                null,
                0,
                out size);

            if (size > 0)
            {
                buffer = new StringBuilder((int)size);

                if (SetupApi.SetupDiGetDeviceRegistryProperty0(
                        hDevInfo,
                        ref deviceInfoData,
                        property,
                        null,
                        buffer,
                        size,
                        null))
                {
                    return(buffer.ToString());
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                return(String.Empty);
            }
        }
Пример #2
0
        public static NameValueCollection EnumDevices(bool PresentOnly, bool ZebraOnly, bool fullDetail)
        {
            NameValueCollection res = new NameValueCollection();
            String name, path, desc, port;

            Guid   intfce;
            IntPtr devs;

            SetupApi.SP_DEVINFO_DATA                 devinfo = new SetupApi.SP_DEVINFO_DATA();
            SetupApi.SP_DEVICE_INTERFACE_DATA        devinterface = new SetupApi.SP_DEVICE_INTERFACE_DATA();
            SetupApi.SP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;
            UInt32 devcount;
            UInt32 size;

            RegistryKey regKey, subKey;

            intfce = GUID_DEVICEINTERFACE_USBPRINT;

            UInt32 flags = SetupApi.DIGCF_DEVICEINTERFACE;

            if (PresentOnly)
            {
                flags |= SetupApi.DIGCF_PRESENT;
            }

            devs = SetupApi.SetupDiGetClassDevs(ref intfce,
                                                null,
                                                IntPtr.Zero,
                                                flags);
            if (devs == (IntPtr)FileIO.INVALID_HANDLE_VALUE)
            {
                return(null);
            }

            devcount            = 0;
            devinterface.cbSize = Marshal.SizeOf(typeof(SetupApi.SP_DEVICE_INTERFACE_DATA));

            while (SetupApi.SetupDiEnumDeviceInterfaces(
                       devs,
                       0,
                       ref intfce,
                       devcount,
                       ref devinterface))
            {
                devcount++;

                SetupApi.SetupDiGetDeviceInterfaceDetail0(
                    devs,
                    ref devinterface,
                    null,
                    0,
                    out size,
                    null);

                if ((size > 0) &&
                    (size <= (Marshal.SizeOf(typeof(SetupApi.SP_DEVICE_INTERFACE_DETAIL_DATA))) - sizeof(UInt32)))
                {
                    interface_detail        = new SetupApi.SP_DEVICE_INTERFACE_DETAIL_DATA();
                    interface_detail.cbSize = (UInt32)(sizeof(UInt32) + sizeof(Char)); // Wow! This is a gotcha!

                    devinfo        = new SetupApi.SP_DEVINFO_DATA();
                    devinfo.cbSize = Marshal.SizeOf(typeof(SetupApi.SP_DEVINFO_DATA));

                    if (SetupApi.SetupDiGetDeviceInterfaceDetail0(
                            devs,
                            ref devinterface,
                            ref interface_detail,
                            size,
                            null,
                            ref devinfo))
                    {
                        path = interface_detail.devicePath.ToString();
                        name = GetDeviceRegistryProperty(devs, ref devinfo, SetupApi.SPDRP_LOCATION_INFORMATION);
                        if (fullDetail)
                        {
                            desc = "";
                            port = "";

                            if (path.StartsWith("\\\\?\\"))
                            {
                                string key = "##?#" + path.Substring(4);

                                try
                                {
                                    regKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\DeviceClasses\\" +
                                                                              GUID_DEVICEINTERFACE_USBPRINT.ToString("B"));
                                    try
                                    {
                                        subKey = regKey.OpenSubKey(key);
                                        if (subKey != null)
                                        {
                                            subKey = subKey.OpenSubKey("#\\Device Parameters");
                                            desc   = subKey.GetValue("Port Description").ToString();
                                            port   = subKey.GetValue("Port Number").ToString();
                                            subKey.Close();
                                        }
                                    }
                                    finally
                                    {
                                        regKey.Close();
                                    }
                                }
                                catch
                                {
                                    // do nothing
                                }
                            }

                            if (ZebraOnly && (!desc.StartsWith("Zebra")))
                            {
                                continue;
                            }

                            res.Add(name, path);
                            res.Add(name, desc);
                            res.Add(name, port);
                        }
                        else
                        {
                            res.Add(name, path);
                        }
                    }
                    else
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
            }
            SetupApi.SetupDiDestroyDeviceInfoList(devs);

            return(res);
        }