Click here to Skip to main content
Click here to Skip to main content

CDeviceTree

By , 2 Apr 2004
 

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.

Figure 2 - Add new tree control to dialog resource

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:

CTreeCtrl   m_DeviceTree;

to:

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. In other hand:

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:

#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):

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
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey26 Feb '12 - 19:49 
Nice
QuestionMicrosoft Kernel System Audio Device?membertigerset9 Dec '10 - 21:58 
HI,I found "Microsoft Kernel System Audio Device" and other devices use your program, but I can't find them in my Device Manager,how to Filter it. thank you!
GeneralCompilation errormemberagsuresh7 Jul '08 - 22:35 
Hi!
 
The program is great. But when I try to compile it I get the following error.
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : error C2146: syntax error : missing ';' before identifier 'RES_DES'
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : fatal error C1004: unexpected end of file found
DeviceTreeDialog.cpp
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : error C2146: syntax error : missing ';' before identifier 'RES_DES'
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : fatal error C1004: unexpected end of file found
DeviceTreeDlg.cpp
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : error C2146: syntax error : missing ';' before identifier 'RES_DES'
c:\program files\microsoft visual studio\vc98\include\cfgmgr32.h(121) : fatal error C1004: unexpected end of file found
 
What could be the problem?
 
regards
suresh
QuestionA QuestionmemberZhang.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?membermadracer26 Dec '05 - 23:39 
Do I just change the inc and lib directories in DDK?
QuestionHow to modify the source code to support win9x/winme?membersams_wang23 Oct '05 - 23:21 
To the title.
AnswerRe: How to modify the source code to support win9x/winme?memberA. Riazi23 Oct '05 - 23:55 
Just use old version of functions, in other hand, instead of using SetupDiOpenClassRegKeyEx(), use SetupDiOpenClassRegKey().
 
Best regards,
A. Riazi

QuestionDevice Tree in VB6 ?memberAPI Novice22 Oct '05 - 2:36 
Confused | :confused: Hello Mr Riazi
Read your article with interest as it is EXACTLY what I want to do, but in Visual BASIC....
I don't have the understanding of Visual C++ to follow your article through to a working project, but I am an experienced VB programmer who knows how to handle Win32 API call etc.....
 
Don't worry if it's a lot of work for you, but I was hoping for some tips for this in a VB6 applic.
 
Thanks anyway,
Phil
AnswerRe: Device Tree in VB6 ?memberA. Riazi22 Oct '05 - 23:46 
If you are an experienced programmer, you know how to incorporate TREE View control in VB.
OK, now only follow my code to employ Setup APIs that I used in my code.
 
Best regards,
A. Riazi
GeneralQuestionsussDong Hoon Lee13 Jul '05 - 2:57 
Hi. At Frist this article is very useful to me. Thanks
 
The sample program is compiled and running without any problem.
But when I adds the function "CM_Request_Device_Eject_Ex" to eject
the USB Mass Storage safely, I got the following error message.
 
"error LNK2001: unresolved external symbol __imp__CM_Request_Device_Eject_ExA@24"
 
I cannot solve this problem by myself.
Do you know the reason of this error message?

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 3 Apr 2004
Article Copyright 2004 by A. Riazi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid