★ wanayoo — archive 1999 http://codeguru.developer.com/shell/shell_namespace.shtmlNouvelle recherche | Portail wanayoo
EarthWeb
Developer.com Click Here!
Click Here!
Find:
 

EXPERT SEARCH
-----
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Contest
mfc codeguru
submission guidelines
 
Book
thinking in c++
stl programmer's guide
reviews
 
Interact
forums
newsgroups
guest book
jobs
jokes
open faq
what's new
 
Other
about
links
newsletters
privacy
tools
 

-----
Visual C++ by E-mail:

Get the weekly e-mail highlights on
Visual C++!
-----



EarthWeb Sites:
CROSSNODES
DATAMATION
DEVELOPER.COM
DICE
DISCUSSIONS
EARTHWEB.COM
EARTHWEB DIRECT
EARTHWEB EVENTS
ERP HUB
GAMELAN
GOCERTIFY
HTML GOODIES
INTRANET JOURNAL
ITKNOWLEDGE
IT NEWS
JARS
JAVASCRIPTS.COM
OPEN SOURCE IT
PERL JOURNAL
SYSOPT.COM
SUPPORTSOURCE
TECHCRAWLER
Y2KINFO

EarthWeb Worldwide

   

Using the Shell Namespace To Get Network Computers, Printers and Recycle Bin Items


This article was contributed by Naveen Kohli.

Sample Image

Environment: VC6

This article explains how to make use of the COM interfaces implemented by Shell Extensions to get the required information on a local machine or network. In this example I have demonstrated it to get the list of all user machines logged onto the network, printers attached to network and items in the recycle bin of local machine. The CShellExt wrapper class can be extended to include the functionality to extract all kind of information. Although there are a lot of wrapper shell functions present to get the same information, but use of COM interfaces gives an insight into what foes on behind the scene in windows explorer and how we can extend the shell's namespace.

Before we ask for any information from Shell, there are couple of environment variables, which needs to be initialized. First we need to allocate memory in Shell's address space. Asking for IMalloc interface pointer, which we will release before we exit our application, can do this. The Desktop folder is the root of Shell's name space, therefore we will need the interface pointer to that. These steps have been implemented in InitializeEnvironment function of CShellExt class.


HRESULT  hr = SHGetMalloc (&m_pMalloc);
HRESULT  hr = SHGetDesktopFolder (&m_pDesktopFolder);

Network Neighborhood, Printers, RecycleBin, etc. fall under the category of Special Folders a.k.a Virtual Folders. Windows API provides bunch of functions to work with them. We use SHGetSpecialFolderPath function to obtain the path to required folder. E.g. to obtain path to printers folder


HRESULT  hr = SHGetSpecialFolderLocation (NULL, CSIDL_PRINTERS, &netItemIdLst);

To get more information about second argument to this function call, look in the on-line help. This argument specifies the special folder for which IShellFolder interface is needed. The link between special folders and their paths is stored in the registry as:

HKEY_CURRENT_USER
 \Software
  \Microsoft
   \Windows
     \CurrentVersion
       \Explorer
        \Shell Folders

After we have IShellFolder interface pointer to special folder, we can ask for contents of folder by creating an item enumeration object (a set of item identifiers) that can be retrieved using the IEnumIDList interface. E.g. for printer folder

HRESULT hr = pPrinterFolder->EnumObjects (NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pEnumIds);

After we have ID list enumeration, the display name for each item can be obtained by using GetDisplayNameOf method of IShellFolder interface.

while ((hr = pEnumIds->Next (1, &pidlItems, &celtFetched)) != S_FALSE && celtFetched == 1) 
{
   hr = pPrinterFolder->GetDisplayNameOf (pidlItems, SHGDN_INFOLDER, &strDispName);
   if (FAILED (hr))
   {
      goto FreeInterfaces;
   }
   .
   .
}

After we have obtained the display name of the folder, we need to extract icons. Every shell object which has icon(s) associated with it, implements IExtractIcon interface. This can be obtained by using GetUIObjectOf method of IShellFolder interface. After getting the IExtractIcon interface pointer, use its two methods to get handles to small and large icons. This has been implemented in GetIconsForFolderNode function of CShellExt class.

IExtractIcon *pExtractIcon = NULL;

hr = pFolderNode->GetUIObjectOf (NULL, 1, const_cast<LPCITEMIDLIST *>(&pidlItems),
IID_IExtractIcon, NULL, reinterpret_cast<LPVOID *>(&pExtractIcon));
	
if (SUCCEEDED (hr)) {
	TCHAR str[MAX_PATH] = {0};
	int index;
	UINT flags;

	// Get the file location where the icons are stored.

	hr = pExtractIcon->GetIconLocation (0, str, MAX_PATH, &index, &flags);
	if (SUCCEEDED (hr)) {
		HICON hiconLarge = NULL;
		HICON hiconSmall = NULL;
		UINT nIconSize = MAKELONG (32, 16);
		LPCSTR pszFile = (LPCSTR)str;

		// Extract the icon handles from the location.

		hr = pExtractIcon->Extract (str, index, &hIconLg, &hIconSm, nIconSize);
		pExtractIcon->Release ();
	}
}

To show the use of this extension class, I have created a dialog-based application, which has a tool bar with 3 buttons for NetWork compuetrs, Printers, RecycleBin items. Click on one of them to see the results. The sample application also shows the usage of CTreeCtrl and how to implement CToolBar in dialog based applications.

Downloads

Download demo project - 28.3 Kb

History

Date Posted: May 21, 1999

Comments:

Add Comment

   

Give them a visit Click Here!

Click Here!

INSTANT EXPERTS!
Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,.

Learn about Free & Open Source software at the Bazaar Conference & Expo!,
December 14-16, 1999 in New York City! Click here for more information.

For tons of Windows technology resources, check out the Windows Programming directory.

Go to Fatbrain for the NEWEST and BEST technical books for IT professionals.

Get Linux Utilities for ONLY $34.95 at EarthWeb Direct.

Give them a visit Click Here!

Click Here!

Click Here!

Click Here!

Home About Us Search Subscribe Advertising Info Contact Us FAQs
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-2000 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.