PrintDesktop函数将桌面的内容全部打印出来。main函数演示了显示我得电脑。
// EnumShellDesk.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <shlobj.h>
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
#pragma comment(lib,"shell32.lib")
//IID_IShellFolder
#define EXPAND_SHLGUID(name, l, w1, w2) EXPAND_GUID(name, l, w1, w2, 0xC0,0,0,0,0,0,0,0x46)
#define EXPAND_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID DECLSPEC_SELECTANY name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
EXPAND_SHLGUID(EXIID_IShellFolder, 0x000214E6L, 0, 0);
int PrintDesktop()
{
LPMALLOC pMalloc;
LPITEMIDLIST pidlProgFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfFirstFolder = NULL;
IShellFolder *psfDeskTop = NULL;
IShellFolder *psfProgFiles = NULL;
LPENUMIDLIST ppenum = NULL;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
ULONG uAttr;
CoInitialize( NULL );
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->QueryInterface(EXIID_IShellFolder,(LPVOID *) &psfProgFiles);
psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS , &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
printf("%s\n",pszDisplayName);
if(!psfFirstFolder)
{
uAttr = SFGAO_FOLDER;
psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr);
if(uAttr & SFGAO_FOLDER)
{
//Here Can Do Enum Sub Folder.
//hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder);
//psfFirstFolder->Release();
//psfFirstFolder = NULL;
}
}
pMalloc->Free(pidlItems);
}
printf("\n",pszDisplayName);
pMalloc->Free(pidlProgFiles);
pMalloc->Release();
psfProgFiles->Release();
CoUninitialize();
return 0;
}
int main(int argc, char* argv[])
{
PrintDesktop();
return 0;
LPMALLOC pMalloc = NULL;
LPITEMIDLIST pidlProgFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfFirstFolder = NULL;
IShellFolder *psfDeskTop = NULL;
IShellFolder *psfProgFiles = NULL;
LPENUMIDLIST ppenum = NULL;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
ULONG uAttr;
CoInitialize( NULL );
hr = SHGetMalloc(&pMalloc);
hr = SHGetFolderLocation(NULL, CSIDL_DRIVES, NULL, NULL, &pidlProgFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlProgFiles, NULL, EXIID_IShellFolder, (LPVOID *) &psfProgFiles);
psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS , &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
printf("%s\n",pszDisplayName);
if(!psfFirstFolder)
{
uAttr = SFGAO_FOLDER;
psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr);
if(uAttr & SFGAO_FOLDER)
{
//hr = psfProgFiles->BindToObject(pidlItems, NULL, EXIID_IShellFolder, (LPVOID *) &psfFirstFolder);
}
}
pMalloc->Free(pidlItems);
}
printf("\n",pszDisplayName);
ppenum->Release();
if(psfFirstFolder)
{
hr = psfFirstFolder->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
psfFirstFolder->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);
printf("%s\n",pszDisplayName);
pMalloc->Free(pidlItems);
}
ppenum->Release();
psfFirstFolder->Release();
}
pMalloc->Free(pidlProgFiles);
pMalloc->Release();
psfProgFiles->Release();
CoUninitialize();
return 0;
}