Esempio n. 1
0
        static unsafe void Main(string[] args)
        {
            int PnPHandle = SetupAPI.SetupDiGetClassDevs(
                null,
                null,
                null,
                SetupAPI.ClassDevsFlags.DIGCF_ALLCLASSES | SetupAPI.ClassDevsFlags.DIGCF_PRESENT
                );

            Console.WriteLine("PnPHandle={0}", PnPHandle);

            int result      = -1;
            int DeviceIndex = 0;

            while (result != 0)
            {
                SetupAPI.SP_DEVINFO_DATA DeviceInfoData = new SetupAPI.SP_DEVINFO_DATA();
                DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);

                result = SetupAPI.SetupDiEnumDeviceInfo(PnPHandle, DeviceIndex, ref DeviceInfoData);

                if (result == 1)
                {
                    int RequiredSize            = 0;
                    SetupAPI.DATA_BUFFER Buffer = new SetupAPI.DATA_BUFFER();
                    result = SetupAPI.SetupDiGetDeviceRegistryProperty(
                        PnPHandle,
                        ref DeviceInfoData,
                        SetupAPI.RegPropertyType.SPDRP_DEVICEDESC,
                        null,
                        ref Buffer,
                        1024,
                        ref RequiredSize
                        );


                    Console.WriteLine("{0}", Buffer.Buffer);
                }

                DeviceIndex++;
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        static unsafe void Main(string[] args)
        {
            Guid UsbGuid = new Guid("{36FC9E60-C465-11CF-8056-444553540000}");

            int PnPHandle = SetupAPI.SetupDiGetClassDevs(
                ref UsbGuid,
                null,
                null,
                SetupAPI.ClassDevsFlags.DIGCF_PRESENT
                );

            int result      = -1;
            int DeviceIndex = 0;

            while (result != 0)
            {
                SetupAPI.SP_DEVINFO_DATA DeviceInfoData = new SetupAPI.SP_DEVINFO_DATA();
                DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
                result = SetupAPI.SetupDiEnumDeviceInfo(PnPHandle, DeviceIndex, ref DeviceInfoData);

                if (result == 1)
                {
                    if (IsRemovable(DeviceInfoData))
                    {
                        Console.WriteLine("{0}", GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_DEVICEDESC));
                        if (SetupAPI.CM_Request_Device_Eject(DeviceInfoData.DevInst, null, null, 0, 0) == 0)
                        {
                            Console.WriteLine("Устройство успешно отключено.");
                        }
                    }
                }

                DeviceIndex++;
            }

            Marshal.FreeHGlobal((System.IntPtr)PnPHandle);

            Console.ReadLine();
        }