Skip to content

Instantly share code, notes, and snippets.

@FrankHB
Last active December 18, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankHB/5711294 to your computer and use it in GitHub Desktop.
Save FrankHB/5711294 to your computer and use it in GitHub Desktop.
Windows shell temp code.
#include <iostream>
#include <shlobj.h>
#include <shlwapi.h>
#include <objbase.h>
//#include <Windows.Foundation.h>
using namespace std;
class COM
{
public:
COM()
{
::CoInitialize(nullptr);
}
~COM()
{
::CoUninitialize();
}
};
int
main()
{
COM com;
LPITEMIDLIST pidlProgFiles = nullptr;
LPITEMIDLIST pidlItems = nullptr;
IShellFolder* psfFirstFolder = nullptr;
IShellFolder* psfDeskTop = nullptr;
IShellFolder* psfProgFiles = nullptr;
LPENUMIDLIST ppenum = nullptr;
ULONG celtFetched;
HRESULT hr;
STRRET strDispName;
TCHAR pszDisplayName[MAX_PATH];
ULONG uAttr;
hr = SHGetFolderLocation(nullptr, CSIDL_PROGRAM_FILES, nullptr, 0, &pidlProgFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlProgFiles, nullptr, IID_IShellFolder, reinterpret_cast<void**>(&psfProgFiles));
psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(nullptr,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);
cout << pszDisplayName << '\n';
if(!psfFirstFolder)
{
uAttr = SFGAO_FOLDER;
psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr);
if(uAttr & SFGAO_FOLDER)
hr = psfProgFiles->BindToObject(pidlItems, nullptr, IID_IShellFolder, reinterpret_cast<void**>(&psfFirstFolder));
}
CoTaskMemFree(pidlItems);
}
cout << "\n\n";
ppenum->Release();
if(psfFirstFolder)
{
hr = psfFirstFolder->EnumObjects(nullptr,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);
cout << pszDisplayName << '\n';
CoTaskMemFree(pidlItems);
}
}
ppenum->Release();
CoTaskMemFree(pidlProgFiles);
psfProgFiles->Release();
psfFirstFolder->Release();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment