我是靠谱客的博主 独特小蝴蝶,最近开发中收集的这篇文章主要介绍枚举桌面的所有对象,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#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, 
00
);

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
;
}

最后

以上就是独特小蝴蝶为你收集整理的枚举桌面的所有对象的全部内容,希望文章能够帮你解决枚举桌面的所有对象所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部