Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C++

Enumerate Properties of an Installed Device

Rate me:
Please Sign up or sign in to vote.
4.84/5 (35 votes)
26 Apr 2004CPOL1 min read 232K   4.8K   65   56
Enumerate properties of an installed device using Setup API.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
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 Acquisition 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 competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions

 
QuestionEnumerate Properties of an Installed Device Pin
Member 1245998523-Apr-21 19:07
Member 1245998523-Apr-21 19:07 
GeneralMy vote of 5 Pin
MArtmath19-Jul-19 21:02
MArtmath19-Jul-19 21:02 
QuestionGood Job but... one question Pin
pitbtons24-Aug-14 23:08
pitbtons24-Aug-14 23:08 
QuestionGooooood~! Pin
saneh8-Oct-12 20:07
saneh8-Oct-12 20:07 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:55
professionalManoj Kumar Choubey26-Feb-12 19:55 
GeneralGet the USB Device ID Pin
bigfatcow1330-Jul-08 20:13
bigfatcow1330-Jul-08 20:13 
Generalget a drive letter from Physical device name (USB) Pin
ranu_hai5-Nov-07 0:55
ranu_hai5-Nov-07 0:55 
Questionreleased .exe Pin
listyanto27-Feb-07 15:19
listyanto27-Feb-07 15:19 
AnswerRe: released .exe Pin
Abbas_Riazi28-Feb-07 21:56
professionalAbbas_Riazi28-Feb-07 21:56 
GeneralRe: released .exe Pin
listyanto1-Mar-07 11:24
listyanto1-Mar-07 11:24 
GeneralFirmware revision number Pin
GreatestMaverick22-Apr-06 22:32
GreatestMaverick22-Apr-06 22:32 
AnswerRe: Firmware revision number Pin
Abbas_Riazi23-Apr-06 3:00
professionalAbbas_Riazi23-Apr-06 3:00 
QuestionWin9x Pin
Morteza Ghasemi22-Apr-06 20:45
Morteza Ghasemi22-Apr-06 20:45 
AnswerRe: Win9x Pin
Hojjat Bohlooli22-Apr-06 20:48
Hojjat Bohlooli22-Apr-06 20:48 
AnswerRe: Win9x Pin
Abbas_Riazi23-Apr-06 3:08
professionalAbbas_Riazi23-Apr-06 3:08 
GeneralDrivers's physical path Pin
macbub15-Sep-05 3:48
macbub15-Sep-05 3:48 
AnswerRe: Drivers's physical path Pin
Abbas_Riazi15-Sep-05 22:13
professionalAbbas_Riazi15-Sep-05 22:13 
GeneralRemove Device Pin
Peter Prescher4-Sep-05 17:29
Peter Prescher4-Sep-05 17:29 
AnswerRe: Remove Device Pin
Abbas_Riazi5-Sep-05 18:51
professionalAbbas_Riazi5-Sep-05 18:51 
GeneralRead properties of a device that is connected through Bluetooth Serial Communications port Pin
ddas-edEn29-Apr-05 3:03
ddas-edEn29-Apr-05 3:03 
GeneralRe: Read properties of a device that is connected through Bluetooth Serial Communications port Pin
Abbas_Riazi29-Apr-05 3:25
professionalAbbas_Riazi29-Apr-05 3:25 
AnswerRe: Read properties of a device that is connected through Bluetooth Serial Communications port Pin
ddas-edEn9-Nov-06 5:07
ddas-edEn9-Nov-06 5:07 
GeneralVB example Pin
adifwww21-Feb-05 10:06
sussadifwww21-Feb-05 10:06 
GeneralRe: VB example [modified] Pin
titusbest8-Aug-06 3:05
titusbest8-Aug-06 3:05 
I have the same problem
adifwww - Did you manage to solve the problem? Maybe you can help me, too.

Thank you


-- modified at 9:05 Tuesday 8th August, 2006
GeneralRe: VB example Pin
David Carta16-Jul-07 13:08
professionalDavid Carta16-Jul-07 13:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.