6

I am developing a windows application which gives the field details --> X.

Where X is -->

Right Click My Computer >

    Properties >

          Device Manager > (select any Item - Say KeyBoard) >

                   Click it > standard PS/2 KeyBoard >

                                double Click standard PS/2 KeyBoard >

                                           click the Details Tab >

Under the Property there are various fields like Display Name , Problem Code,Parent Siblings, etc , etc?

I want to get their values . Which Windows API I can use for this. I am doing this for windows 7 as well as windows 8.I hope the API will remain the same.Also i am having 64 bit machine. This has to be true for any device whose details I wanted to know from the Device Manager.

ALso I just want to all operations - Reading and No Set (writing) so I think I will not be having any problem with violating the Admin Rights.PLease suggest.! I have added Snapshots for reference!Say for example I want to know the current State of the HID USB Complaint Mouse(D0(Active) or D2(Sleep)).

Image Showing the Powerdata Field for HID Compliant Mouse

Image Showing the Power State for HID Complaint Mouse That is D0 - Active

I need to Get this Power State D0.

Raulp
  • 7,758
  • 20
  • 93
  • 155

3 Answers3

4

The question is tagged with C#, though the actual question asks for any Window API. With the Win32 API the information can be retrieved with SetupDiGetDeviceRegistryProperty(). The steps would be:

  1. Get a device info set for the devices you're interested in via SetupDiGetClassDevs().
  2. Iterate through the device infos via SetupDiEnumDeviceInfo().
  3. Get the properties via calls to SetupDiGetDeviceRegistryProperty().
  4. Destroy the device info set via SetupDiDestroyDeviceInfoList().

According to the documentation the API is available on Windows 2000 and later.

user686249
  • 669
  • 6
  • 17
  • This is the correct answer. To find the power state as requested in the question you need to query SPDRP_DEVICE_POWER_DATA using SetupDiGetDeviceRegistryProperty. – donovan Sep 17 '14 at 06:20
3

It's quite easy to get the hardware information using ManagementObjectCollection.

For instance to get all properties and values from the PC processor

var win32DeviceClassName = "win32_processor";
            var query = string.Format("select * from {0}", win32DeviceClassName);

            using (var searcher = new ManagementObjectSearcher(query))
            {
                ManagementObjectCollection  objectCollection = searcher.Get();

                foreach (ManagementBaseObject managementBaseObject in objectCollection)
                {
                    foreach (PropertyData propertyData in managementBaseObject.Properties)
                    {
                        Console.WriteLine("Property:  {0}, Value: {1}", propertyData.Name, propertyData.Value);
                    }
                }



            }

The full list of WIN32 class name is available at http://msdn.microsoft.com/en-us/library/aa394084%28v=VS.85%29.aspx

Cheers.

Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59
  • I am runnning x64 , still I have to use the string "var win32DeviceClassName = "win32_processor";" – Raulp Mar 06 '13 at 08:40
  • The code snippet here is supposed to do what you asked. Yes, copy it to the main function of your C# console project. It will enumerate all properties of your processor. – Toan Nguyen Mar 07 '13 at 05:02
  • Do you know why I get a type mismatch when trying to set a DateTime object to the "InstallDate" property? – CodeMonkey Aug 27 '13 at 12:03
  • While WMI objects have some of the same info, they don't provide a way to query all of the properties shown in the Device Manager that I can find (as per the question asked). – donovan Sep 16 '14 at 07:06
  • I did - WMI simply doesn't give you access to all the same state that is available in the device manager. The answer listed using SetupDiGetClassDevs & SetupDiGetDeviceRegistryProperty is the correct answer. All the state items shown in Device Manager are available using SetupDiGetDeviceRegistryProperty. – donovan Sep 17 '14 at 06:18
1

You're going to have the easiest time (I think) doing this with PowerShell. If you are writing some C# code you can execute a PS script using types in the System.Management.Automation namespace, such as PowerShell (link: http://msdn.microsoft.com/en-us/library/system.management.automation.powershell(v=vs.85).aspx) but I would begin your testing using the PS Console.

You should first (using PowerShell) explore the WMI objects in your environments using this command

Get-WmiObject -List -namespace root\CIMV2

Then once you identify with class you are looking for you can retrieve details on that class using this command:

Get-WmiObject -namespace root\CIMV2 -class Win32_USBControllerDevice

Once you have that content you'd have to parse the text.

UPDATE: Try using this command to get the "State", "Status", and "Started" attributes of mouse drivers on your PC:

gwmi Win32_SystemDriver | where {$_.DisplayName -like "*Mouse*"}
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • Thanks for this but I have improved the Question.What i Want is Quiet Different from What you propsed.With Power Shell I can get the Huge List of All USB Devices and thier properties whcih are not What I asked, I guess.PLease have a look at the snapshots for better clarity of the question. – Raulp Mar 06 '13 at 08:54
  • With the correct query you can get that precise value. I can refine my answer – Glenn Ferrie Mar 07 '13 at 14:11
  • Tha above command for Mouse doesnt gives the variable like " Power Data? – Raulp Jan 28 '14 at 09:07
  • As with the other WMI answer, WMI doesn't give you access to all the same state. – donovan Sep 17 '14 at 06:21