Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

CDeviceTree

Rate me:
Please Sign up or sign in to vote.
4.83/5 (26 votes)
2 Apr 20042 min read 197.6K   6.2K   62   43
A tree control to show installed devices like Device Manager

Sample Image - DeviceTree.jpg

Introduction

Here, we want to show installed devices of our PC like what a Device Manager shows. In Device Manager, installed devices are shown in specific categories like: DVD/CD-ROM Drives or Floppy disk controllers. In my previous article, Enumerate Installed Devices Using Setup API, I show you how we can list all devices. In this article, installed devices are shown in a tree in their categories, like Device Manager.

For this purpose, I created a new class called CDeviceTree that is inherited from CTreeCtrl. Using of the new class is very simple.

How to Use

First of all, add DeviceTree.h and DeviceTree.cpp files to your project. Then in your dialog resource editor, add new Tree control (figure 2 shows it). Rename the control ID to your desired ID, for example IDC_DEVICE_TREE. Run class wizard (Ctrl+W) and add new member variable with variable type CDeviceTree. (If CDeviceTree does not appear in combo box, choose CTreeCtrl, then in your dialog header file, rename CTreeCtrl to CDeviceTree). Name it as m_DeviceTree.

Image 2

Figure 2: Add new tree control to dialog resource

Image 3

Figure 3: Add member variable with variable type CDeviceTree.

If CDeviceTree does not appear in Variable type combo box, simply choose CTreeCtrl, then rename it to CDeviceTree in your dialog header file. For example:

C++
CTreeCtrl   m_DeviceTree;

to:

C++
CDeviceTree   m_DeviceTree;

Don't forget to include DeviceTree.h to your dialog header file. OK, now everything is ready to compile!

CDeviceTree Class

The CDeviceTree class contains only one public member function: EnumDevices(). The function is responsible for enumerating all of the installed devices in their categories. The user must only use this function to see the tree of devices. The best way for this is OnInitDialog() member function of main dialog. On the other hand:

C++
BOOL CYourDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    m_DeviceTree.EnumDevices();   //CDeviceTree will enumerate all of devices
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

The class definition is as below:

C++
#ifndef _DEVICE_TREE_H_
#define _DEVICE_TREE_H_

#include "SetupAPI.h"
#include "cfgmgr32.h"

class CDeviceTree : public CTreeCtrl
{
// Construction
public:
    CDeviceTree();

// Implementation
public:
    void EnumDevices();
    virtual ~CDeviceTree();

private:
    int EnumDevices(int index, TCHAR* DeviceClassName, TCHAR* DeviceName);
    int EnumDeviceClasses(int index, TCHAR* DeviceClassName, 
              TCHAR* DeviceClassDesc, BOOL* DevicePresent, int* ClassImage);
    
    SP_CLASSIMAGELIST_DATA m_ImageListData;
    CImageList m_ImageList;
    
    // Generated message map functions
protected:
    //{{AFX_MSG(CDeviceTree)
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

#endif // _DEVICE_TREE_H_

As you see, the application shows devices like Device Manager does (Figure 4):

Image 4

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.


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

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:49
professionalManoj Kumar Choubey26-Feb-12 19:49 
QuestionMicrosoft Kernel System Audio Device? Pin
tigerset9-Dec-10 21:58
tigerset9-Dec-10 21:58 
GeneralCompilation error Pin
agsuresh7-Jul-08 22:35
agsuresh7-Jul-08 22:35 
QuestionA Question Pin
Zhang.W23-Sep-07 23:25
Zhang.W23-Sep-07 23:25 
Hello, A. Riazi

your Program has a question of efficiency, I wait a long time to Display the result, I need the CDeviceTree control,Can you optimize it for me?

thanks!
QuestionHow to compile the source code for x64 system? Pin
madracer26-Dec-05 23:39
madracer26-Dec-05 23:39 
QuestionHow to modify the source code to support win9x/winme? Pin
c.s.wang23-Oct-05 23:21
c.s.wang23-Oct-05 23:21 
AnswerRe: How to modify the source code to support win9x/winme? Pin
Abbas_Riazi23-Oct-05 23:55
professionalAbbas_Riazi23-Oct-05 23:55 
QuestionDevice Tree in VB6 ? Pin
API Novice22-Oct-05 2:36
API Novice22-Oct-05 2:36 
AnswerRe: Device Tree in VB6 ? Pin
Abbas_Riazi22-Oct-05 23:46
professionalAbbas_Riazi22-Oct-05 23:46 
GeneralQuestion Pin
Dong Hoon Lee13-Jul-05 2:57
sussDong Hoon Lee13-Jul-05 2:57 
GeneralRe: Question Pin
Dong Hoon Lee13-Jul-05 22:38
sussDong Hoon Lee13-Jul-05 22:38 
GeneralRe: Question Pin
Neeranjan7-Dec-07 1:04
Neeranjan7-Dec-07 1:04 
Generalhelp me Pin
Member 207096225-Jun-05 10:28
Member 207096225-Jun-05 10:28 
GeneralRe: help me Pin
Abbas_Riazi25-Jun-05 20:34
professionalAbbas_Riazi25-Jun-05 20:34 
GeneralMistakes at compilation Pin
nina194710-Mar-05 3:29
nina194710-Mar-05 3:29 
GeneralRe: Mistakes at compilation Pin
Abbas_Riazi10-Mar-05 23:44
professionalAbbas_Riazi10-Mar-05 23:44 
GeneralRe: Mistakes at compilation Pin
queylai5-Mar-12 0:08
queylai5-Mar-12 0:08 
GeneralDevices by connection... Pin
IanTJ24-Feb-05 7:27
IanTJ24-Feb-05 7:27 
GeneralRe: Devices by connection... Pin
Abbas_Riazi25-Feb-05 5:56
professionalAbbas_Riazi25-Feb-05 5:56 
GeneralRe: Devices by connection... Pin
IanTJ25-Feb-05 8:58
IanTJ25-Feb-05 8:58 
GeneralDelay... Pin
Robert Buldoc27-Oct-04 12:29
Robert Buldoc27-Oct-04 12:29 
GeneralRe: Delay... Pin
Abbas_Riazi27-Oct-04 21:38
professionalAbbas_Riazi27-Oct-04 21:38 
GeneralRe: Delay... Pin
Tam2Lana13-Oct-06 7:50
Tam2Lana13-Oct-06 7:50 
GeneralMore like device manager Pin
daDude22-Sep-04 5:56
daDude22-Sep-04 5:56 
GeneralRe: More like device manager Pin
Abbas_Riazi23-Sep-04 1:15
professionalAbbas_Riazi23-Sep-04 1:15 

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.