完整的通过ShellAPI枚举桌面目录的代码

完整的通过ShellAPI枚举桌面目录的代码

为什么要使用ShellAPI来枚举桌面,而不是通过FindFirstFile呢?只有自己尝试过的人才能明白。而ShellAPI的使用又相对晦涩,在一番研究后终于搞明白了。特写下试验代码,以供有类似需求的人参考。

int _tmain(int argc, _TCHAR* argv[])
{
    LPMALLOC pMalloc;

    LPITEMIDLIST pidlItems = NULL;
    IShellFolder *psfDeskTop = NULL;
    LPENUMIDLIST ppenum = NULL;
    ULONG celtFetched;
    HRESULT hr;
    STRRET strDispName;
    TCHAR pszDisplayName[MAX_PATH];

    CoInitialize( NULL );
    hr = SHGetMalloc(&pMalloc);
    hr = SHGetDesktopFolder(&psfDeskTop);

    hr = psfDeskTop->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);

    int idx = 0;
    while( (hr = ppenum->Next(1,&pidlItems, &celtFetched)) == S_OK && (celtFetched) == 1)
    {

        //psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDispName);        //得到相对与Desktop的路径名
        //psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);    //得到相对于folder from which the request was made.
        psfDeskTop->GetDisplayNameOf(pidlItems, SHGDN_FORPARSING, &strDispName);
        StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH);


        cout << idx++ << " - " << pszDisplayName << '/n';

        //在资源管理器中打开并选中
        //SHOpenFolderAndSelectItems (pidlItems, 0, NULL, 0);

        //通过SHFileOperation可以完成copy delete move rename操作

        //通过ShellExecuteEx完成"打开"操作,即Execute
        {
            SHELLEXECUTEINFO ShExecInfo;
            ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
            ShExecInfo.fMask = NULL;
            ShExecInfo.hwnd = NULL;
            ShExecInfo.lpVerb = NULL;
            ShExecInfo.lpFile = pszDisplayName;
            ShExecInfo.lpParameters = NULL;
            ShExecInfo.lpDirectory = NULL;
            ShExecInfo.nShow = SW_MAXIMIZE;
            ShExecInfo.hInstApp = NULL;

            ShellExecuteEx(&ShExecInfo);
        }

        pMalloc->Free(pidlItems);
    }

    cout << "/n/n";

    ppenum->Release();
    pMalloc->Release();
    psfDeskTop->Release();

    CoUninitialize();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值