Home www.visualprog.cz

.NET C# Win. application to detect active HW device by GUID of DeviceClasses
(c)2010 Pavel Pindora
Send us your question
 
Příklad (Example: all disk devices in the system)
Seznam všech (active/non active) diskových zařízení v DeviceClasses,
který se nachází v registrech HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\DeviceClasses
pod klíčem (Key) 53f56307-b6bf-11d0-94f2-00a0c91efb8b
 
DetectDev1
 
WApp_HW_List.exe

detekuje aktivní zařízení.

detects the active device.

 
Form1 [Design]
DetectDev1
 
53f56307-b6bf-11d0-94f2-00a0c91efb8b

Detekováno
Detected
\\?\ide#diskwdc_wd3200bevt-60zct0___________________12.01a12#5&1b30e775&0&0.0.0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
 
ad498944-762f-11d0-8dcb-00c04fc3358c

Detekováno
Detected
Count of Device Interface Detail:23
First Device Detail:
\\?\bth#ms_bthpan#6&6964636&0&2#{ad498944-762f-11d0-8dcb-00c04fc3358c}\{0ceef848-2d2b-4048-923a-183eaae9ae9a}
Source code
class Form1
 

#region usings

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

#endregion usings

 

namespace WApp_HW_List

{

    public partial class Form1 : Form

    {

        const string GUID_DEVINTERFACE_DISK         = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";

        const int   DIGCF_PROFILE                   = (0x00000008),

                    DIGCF_PRESENT                   = (0x00000002),

                    DIGCF_DEVICEINTERFACE           = (0x00000010),

                    INVALID_HANDLE_VALUE            = -1;

 

        public Form1()

        {

            InitializeComponent();

        }

 

        #region Event button1_Click Fill

        private void button1_Click(object sender, EventArgs e)

        {

            textBox1.Text = Environment.StackTrace;

            List<string> devicePathName = new List<string>();

 

            #region Get GUID by radioB

            Guid    DiskGUID = new Guid(GUID_DEVINTERFACE_DISK);

            if( radioButton2.Checked ) DiskGUID = new Guid("4d36e972e32511cebfc108002be10318");

            if( radioButton3.Checked ) Win32Calls.HidD_GetHidGuid(out DiskGUID);

            if( radioButton4.Checked ) DiskGUID = new Guid(textBoxYouGuid.Text);

            #endregion Get GUID by radioB

 

            #region Get Devs

            IntPtr h = Win32Calls.SetupDiGetClassDevs(ref DiskGUID, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_PROFILE | DIGCF_DEVICEINTERFACE);

            uint glerr = Win32Calls.GetLastError();

            if (h.ToInt32() != INVALID_HANDLE_VALUE)

            {

                bool Success = true;

                uint i = 0;

                while (Success)

                {

                    Win32Calls.SP_DEVICE_INTERFACE_DATA dia = new Win32Calls.SP_DEVICE_INTERFACE_DATA();

                    dia.cbSize = Marshal.SizeOf(dia);

 

                    Success = Win32Calls.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia);

                    glerr = Win32Calls.GetLastError();

                    textBox1.Text = "SetupDiEnumDeviceInterfaces Success:" + Success.ToString() + " LastError:" + glerr.ToString();

                    if (Success)

                    {

                        IntPtr detailDataBuffer = IntPtr.Zero;

                        Int32 bufferSize = 0;

                        Win32Calls.SP_DEVINFO_DATA da = new Win32Calls.SP_DEVINFO_DATA();

                        da.cbSize = Marshal.SizeOf(da);

 

                        Win32Calls.SP_DEVICE_INTERFACE_DETAIL_DATA didd = new Win32Calls.SP_DEVICE_INTERFACE_DETAIL_DATA();

                        didd.DevicePath = new byte[Win32Calls.ANYSIZE_ARRAY];

 

 

                        didd.cbSize = 4 + Marshal.SystemDefaultCharSize;

                        uint nBytes = (uint)didd.cbSize;

 

                        Success = Win32Calls.SetupDiGetDeviceInterfaceDetail(h, ref dia, IntPtr.Zero, 0     , ref bufferSize    , IntPtr.Zero);

                        detailDataBuffer = Marshal.AllocHGlobal(bufferSize);

                        Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

                        nBytes = (uint)bufferSize;

 

                        Success = Win32Calls.SetupDiGetDeviceInterfaceDetail(h, ref dia, detailDataBuffer, nBytes, ref bufferSize, IntPtr.Zero);

                        glerr = Win32Calls.GetLastError();

                        textBox1.Text = "SetupDiGetDeviceInterfaceDetail Success:" + Success.ToString() + " LastError:" + glerr.ToString();

                        if (Success)

                        {

                            IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);

                            devicePathName.Add( Marshal.PtrToStringAuto(pDevicePathName) );

 

                            IntPtr ptrInstanceBuf = Marshal.AllocHGlobal((Int32)nBytes);

                            Win32Calls.CM_Get_Device_ID((UInt32)pDevicePathName.ToInt32(), ptrInstanceBuf, (Int32)nBytes, 0);

                            string InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

                            Marshal.FreeHGlobal(ptrInstanceBuf);

                        }

                    }

                    i++;

                }

            }

            else

            {

                textBox1.Text = "SetupDiGetClassDevs LastError:" + glerr.ToString();

            }

            #endregion Get Devs

 

            if (devicePathName.Count > 0)

            {

                textBox1.Text = "Count of Device Interface Detail:" + devicePathName.Count.ToString() + " First Device Detail:" + devicePathName[0];

                for (int i = 0; i < devicePathName.Count; i++)

                {

                    DataGridViewRow dgrc = new DataGridViewRow();

                    DataGridViewCell dgvc;

                    dgvc = new DataGridViewTextBoxCell();

                    dgvc.Value = (dataGridView1.Rows.Count + 1).ToString();//i.ToString();

                    dgrc.Cells.Add(dgvc);

                    dgvc = new DataGridViewTextBoxCell();

                    dgvc.Value = devicePathName[i];

                    dgrc.Cells.Add(dgvc);

 

                    dataGridView1.Rows.Add(dgrc);

                }

           }

           bool okDestroy = Win32Calls.SetupDiDestroyDeviceInfoList(h);

        }

        #endregion Event button1_Click Fill

 

        #region Event button1_Click Clear

        private void button2_Click(object sender, EventArgs e)

        {

            dataGridView1.Rows.Clear();

        }

        #endregion Event button1_Click Clear

 

 

    }

 

    #region Win32Calls

    class Win32Calls

    {

        public static uint ANYSIZE_ARRAY = 1000;

 

        [StructLayout(LayoutKind.Sequential)]

        public struct SP_DEVINFO_DATA

        {

            public int cbSize;

            public Guid ClassGuid;

            public uint DevInst;

            public IntPtr Reserved;

        }

 

        [StructLayout(LayoutKind.Sequential)]

        public struct SP_DEVICE_INTERFACE_DATA

        {

            public int cbSize;

            public Guid InterfaceClassGuid;

            public uint Flags;

            public IntPtr Reserved;

        }

 

        [StructLayout(LayoutKind.Sequential)]

        public struct SP_DEVICE_INTERFACE_DETAIL_DATA

        {

            public int   cbSize;

            public byte []  DevicePath;

        }

 

        [DllImport(@"Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern uint GetLastError();

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, int Enumerator, IntPtr hwndParent, UInt32 Flags);

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr nSetupDiGetClassDevs, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, uint DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVINFO_DATA DeviceInfoData);

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr nSetupDiGetClassDevs, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr Ptr, uint DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr PtrInfo);

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern uint CM_Get_Parent(out UInt32 pdnDevInst, uint dnDevInst, ulong ulFlags);

        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]

        public static extern int CM_Get_Device_ID( UInt32 dnDevInst,IntPtr Buffer,int BufferLen,int ulFlags);

        [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);

        [DllImport("hid.dll", SetLastError = true)]

        public static extern void HidD_GetHidGuid(out Guid gHid);

    }

    #endregion Win32Calls

}