5,666,979 members and growing! (16,831 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate

Enumerate Properties of an Installed Device

By A. Riazi

Enumerate properties of an installed device using Setup API.
VC6, C++Windows, Win2KVS6, Visual Studio, Dev

Posted: 26 Apr 2004
Updated: 26 Apr 2004
Views: 82,055
Bookmarked: 31 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
27 votes for this Article.
Popularity: 6.03 Rating: 4.21 out of 5
2 votes, 7.4%
1
0 votes, 0.0%
2
1 vote, 3.7%
3
4 votes, 14.8%
4
20 votes, 74.1%
5

Sample Image - EnumDeviceProperties.jpg

Introduction

In the previous article, I showed you how you can enumerate installed devices. In this article, we want to enumerate properties of an installed device. For this purpose, we use Setup API. You must have the latest platform SDK and also DDK to compile the demo application.

Setup API

The Setup application programming interface (API) provides a set of functions that your setup application can call to perform installation operations or get several information about installed devices, their class, properties and also their GUID (a unique identifier for every device).

The application requires the following APIs (description of these APIs was taken from MSDN):

CMAPI CONFIGRET WINAPI CM_Get_Device_ID_Ex(DEVINST  dnDevInst, PTCHAR Buffer, 
    ULONG BufferLen, ULONG ulFlags, HMACHINE Machine);

The CM_Get_Device_ID_Ex function retrieves the device instance ID for a specified device instance, on a local or remote machine.

CMAPI CONFIGRET WINAPI CM_Get_DevNode_Status_Ex(PULONG  pulStatus, 
    PULONG  pulProblemNumber, DEVINST  dnDevInst, 
    ULONG  ulFlags, HMACHINE hMachine);

The CM_Get_DevNode_Status_Ex function obtains the status of a device instance from its device node, on a local or a remote machine's device tree.

DWORD CM_Get_DevNode_Registry_Property_Ex(DEVINST dnDevInst, ULONG ulProperty, 
    PULONG pulRegDataType, PVOID Buffer, PULONG pulLength, 
    LONG ulFlags, HMACHINE hMachine);

The CM_Get_DevNode_Registry_Property_Ex function retrieves a specified device property from the registry.

Solution

Enumerating properties of an installed device is done by only two functions. The first one specifies the property and then calls the second function. The second function calls CM_Get_DevNode_Registry_Property_Ex function to retrieve property from registry and format it in a good manner.

Here are the two functions:

void EnumDeviceProperties(DEVNODE dn)
{
    int BufferSize = MAX_PATH + MAX_DEVICE_ID_LEN;
    TCHAR Buffer[MAX_PATH + MAX_DEVICE_ID_LEN];
    CString Temp;
    
    DeviceProperties Properties[26]=
    {
        ID_DEVICEID,            _T("Device ID: "), _T(""),
        ID_STATUS,              _T("Status: "), _T(""),
        ID_PROBLEM,             _T("Problem: "), _T(""),
        ID_SERVICE,             _T("Service: "), _T(""),
        ...
    };

    if (CM_Get_Device_ID_Ex(dn, Buffer, BufferSize, 0, m_hMachine) 
            == CR_SUCCESS)
    {
        Temp=Buffer;
    }
    else
    {
        Temp=_T("Fail to retrieve Device ID");
    }

    ULONG Status, Problem;

    if (CM_Get_DevNode_Status_Ex(&Status, &Problem, dn, 0, m_hMachine) 
            == CR_SUCCESS)
    {
        Temp.Format(_T("0x%08x"), Status);
        Temp.Format(_T("0x%08x"), Problem);
    }
    else
    {
        Temp=_T("Fail to retrieve Device Status/Problem");
    }


    Temp=GetProperty(dn, CM_DRP_SERVICE);
    
    Temp=GetProperty(dn, CM_DRP_CAPABILITIES);
    
    ...
}


CString GetProperty(DEVNODE dn, ULONG Property)
{
    CString Temp;
    
    TCHAR Buffer[REGSTR_VAL_MAX_HCID_LEN]=_T("");    
    ULONG Type;
    ULONG Size = sizeof(Buffer);
    
    if (CM_Get_DevNode_Registry_Property_Ex(dn, Property,
                        &Type,
                        Buffer,
                        &Size,
                        0, m_hMachine) == CR_SUCCESS)
    {
        if (Type == REG_DWORD || 
            Type == REG_MULTI_SZ || 
            Type == REG_SZ )
        {
            if (Type == REG_DWORD)
            {
                DWORD Data = *((DWORD*)Buffer);
                wsprintf(Buffer, _T("0x%08x"), *((DWORD*) Buffer) );
            }
            else if (Type == REG_MULTI_SZ)
            {
                LPTSTR p = Buffer;
                while (_T('\0') != *p)
                {
                    p += lstrlen(p);
                    if (_T('\0') != *p)
                    *p++ = _T(',');
                }
            }
        }
    }

    Temp=Buffer;
    return Temp;
}

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

A. Riazi


I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:


You can see list of my articles, by clicking here


Occupation: Software Developer (Senior)
Company: Misbah3Com
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 50 (Total in Forum: 50) (Refresh)FirstPrevNext
GeneralGet the USB Device IDmemberbigfatcow1321:13 30 Jul '08  
Generalget a drive letter from Physical device name (USB)memberranu_hai1:55 5 Nov '07  
Questionreleased .exemember16:19 27 Feb '07  
AnswerRe: released .exememberA. Riazi22:56 28 Feb '07  
GeneralRe: released .exememberlistyanto12:24 1 Mar '07  
GeneralFirmware revision numbermemberGreatestMaverick23:32 22 Apr '06  
AnswerRe: Firmware revision numbermemberA. Riazi4:00 23 Apr '06  
QuestionWin9xmemberMorteza Ghasemi21:45 22 Apr '06  
AnswerRe: Win9xmemberhojjat_rele21:48 22 Apr '06  
AnswerRe: Win9xmemberA. Riazi4:08 23 Apr '06  
GeneralDrivers's physical pathmembermacbub4:48 15 Sep '05  
AnswerRe: Drivers's physical pathmemberA. Riazi23:13 15 Sep '05  
GeneralRemove DevicememberPeter Prescher18:29 4 Sep '05  
AnswerRe: Remove DevicememberA. Riazi19:51 5 Sep '05  
GeneralRead properties of a device that is connected through Bluetooth Serial Communications portmemberddas774:03 29 Apr '05  
GeneralRe: Read properties of a device that is connected through Bluetooth Serial Communications portmemberA. Riazi4:25 29 Apr '05  
AnswerRe: Read properties of a device that is connected through Bluetooth Serial Communications portmemberddas776:07 9 Nov '06  
GeneralVB examplesussadifwww11:06 21 Feb '05  
GeneralRe: VB example [modified]membertitusbest4:05 8 Aug '06  
GeneralRe: VB examplememberDavid Carta14:08 16 Jul '07  
GeneralRe: VB examplememberMichaelino7:18 14 Sep '07  
Generalexcellent article- how to get drive letter for USB device?membersubash11076:13 11 Oct '04  
GeneralRe: excellent article- how to get drive letter for USB device?memberA. Riazi8:20 11 Oct '04  
GeneralRe: excellent article- how to get drive letter for USB device?membersubash110720:22 11 Oct '04  
GeneralHow to declare the cfgmgr32.h?susstsung_yu22:45 3 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by A. Riazi
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project