Created
February 11, 2020 12:06
-
-
Save t-mat/6561f0fc5685ac587e515b00cff2e3af to your computer and use it in GitHub Desktop.
[WIN32] Shell API KNOWNFOLDERID example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
WIN32 Shell API KNOWNFOLDERID example | |
Wait. What has happned to CSIDL? | |
It's retired. CSIDL has the following note: | |
https://docs.microsoft.com/en-us/windows/win32/shell/csidl | |
> As of Windows Vista, these values have been replaced by KNOWNFOLDERID values. | |
KNOWNFOLDERID: | |
https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid | |
There're registry entries: | |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\FolderDescriptions | |
*/ | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <shlobj.h> // REFKNOWNFOLDERID, SHGetKnownFolderPath | |
#include <KnownFolders.h> // FOLDERID_Documents | |
#include <string> // std::wstring | |
// id : https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid?redirectedfrom=MSDN | |
// flags : https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/ne-shlobj_core-known_folder_flag | |
// return : The returned path does not include a trailing backslash. | |
std::wstring getKnownFolderPath(REFKNOWNFOLDERID id, DWORD flags = 0) { | |
std::wstring result; | |
wchar_t* path = nullptr; | |
const auto hr = SHGetKnownFolderPath(id, flags, nullptr, &path); | |
if(SUCCEEDED(hr)) { | |
result = std::wstring(path); | |
} | |
if(path) { CoTaskMemFree(path); } | |
return result; | |
} | |
struct Entry { | |
KNOWNFOLDERID knownFolderId; | |
const wchar_t* name; | |
}; | |
const Entry entries[] = { | |
{ FOLDERID_NetworkFolder , L"NetworkFolder" }, | |
{ FOLDERID_ComputerFolder , L"ComputerFolder" }, | |
{ FOLDERID_InternetFolder , L"InternetFolder" }, | |
{ FOLDERID_ControlPanelFolder , L"ControlPanelFolder" }, | |
{ FOLDERID_PrintersFolder , L"PrintersFolder" }, | |
{ FOLDERID_SyncManagerFolder , L"SyncManagerFolder" }, | |
{ FOLDERID_SyncSetupFolder , L"SyncSetupFolder" }, | |
{ FOLDERID_ConflictFolder , L"ConflictFolder" }, | |
{ FOLDERID_SyncResultsFolder , L"SyncResultsFolder" }, | |
{ FOLDERID_RecycleBinFolder , L"RecycleBinFolder" }, | |
{ FOLDERID_ConnectionsFolder , L"ConnectionsFolder" }, | |
{ FOLDERID_Fonts , L"Fonts" }, | |
{ FOLDERID_Desktop , L"Desktop" }, | |
{ FOLDERID_Startup , L"Startup" }, | |
{ FOLDERID_Programs , L"Programs" }, | |
{ FOLDERID_StartMenu , L"StartMenu" }, | |
{ FOLDERID_Recent , L"Recent" }, | |
{ FOLDERID_SendTo , L"SendTo" }, | |
{ FOLDERID_Documents , L"Documents" }, | |
{ FOLDERID_Favorites , L"Favorites" }, | |
{ FOLDERID_NetHood , L"NetHood" }, | |
{ FOLDERID_PrintHood , L"PrintHood" }, | |
{ FOLDERID_Templates , L"Templates" }, | |
{ FOLDERID_CommonStartup , L"CommonStartup" }, | |
{ FOLDERID_CommonPrograms , L"CommonPrograms" }, | |
{ FOLDERID_CommonStartMenu , L"CommonStartMenu" }, | |
{ FOLDERID_PublicDesktop , L"PublicDesktop" }, | |
{ FOLDERID_ProgramData , L"ProgramData" }, | |
{ FOLDERID_CommonTemplates , L"CommonTemplates" }, | |
{ FOLDERID_PublicDocuments , L"PublicDocuments" }, | |
{ FOLDERID_RoamingAppData , L"RoamingAppData" }, | |
{ FOLDERID_LocalAppData , L"LocalAppData" }, | |
{ FOLDERID_LocalAppDataLow , L"LocalAppDataLow" }, | |
{ FOLDERID_InternetCache , L"InternetCache" }, | |
{ FOLDERID_Cookies , L"Cookies" }, | |
{ FOLDERID_History , L"History" }, | |
{ FOLDERID_System , L"System" }, | |
{ FOLDERID_SystemX86 , L"SystemX86" }, | |
{ FOLDERID_Windows , L"Windows" }, | |
{ FOLDERID_Profile , L"Profile" }, | |
{ FOLDERID_Pictures , L"Pictures" }, | |
{ FOLDERID_ProgramFilesX86 , L"ProgramFilesX86" }, | |
{ FOLDERID_ProgramFilesCommonX86 , L"ProgramFilesCommonX86" }, | |
{ FOLDERID_ProgramFilesX64 , L"ProgramFilesX64" }, | |
{ FOLDERID_ProgramFilesCommonX64 , L"ProgramFilesCommonX64" }, | |
{ FOLDERID_ProgramFiles , L"ProgramFiles" }, | |
{ FOLDERID_ProgramFilesCommon , L"ProgramFilesCommon" }, | |
{ FOLDERID_UserProgramFiles , L"UserProgramFiles" }, | |
{ FOLDERID_UserProgramFilesCommon, L"UserProgramFilesCommon" }, | |
{ FOLDERID_AdminTools , L"AdminTools" }, | |
{ FOLDERID_CommonAdminTools , L"CommonAdminTools" }, | |
{ FOLDERID_Music , L"Music" }, | |
{ FOLDERID_Videos , L"Videos" }, | |
{ FOLDERID_Ringtones , L"Ringtones" }, | |
{ FOLDERID_PublicPictures , L"PublicPictures" }, | |
{ FOLDERID_PublicMusic , L"PublicMusic" }, | |
{ FOLDERID_PublicVideos , L"PublicVideos" }, | |
{ FOLDERID_PublicRingtones , L"PublicRingtones" }, | |
{ FOLDERID_ResourceDir , L"ResourceDir" }, | |
{ FOLDERID_LocalizedResourcesDir , L"LocalizedResourcesDir" }, | |
{ FOLDERID_CommonOEMLinks , L"CommonOEMLinks" }, | |
{ FOLDERID_CDBurning , L"CDBurning" }, | |
{ FOLDERID_UserProfiles , L"UserProfiles" }, | |
{ FOLDERID_Playlists , L"Playlists" }, | |
{ FOLDERID_SamplePlaylists , L"SamplePlaylists" }, | |
{ FOLDERID_SampleMusic , L"SampleMusic" }, | |
{ FOLDERID_SamplePictures , L"SamplePictures" }, | |
{ FOLDERID_SampleVideos , L"SampleVideos" }, | |
{ FOLDERID_PhotoAlbums , L"PhotoAlbums" }, | |
{ FOLDERID_Public , L"Public" }, | |
{ FOLDERID_ChangeRemovePrograms , L"ChangeRemovePrograms" }, | |
{ FOLDERID_AppUpdates , L"AppUpdates" }, | |
{ FOLDERID_AddNewPrograms , L"AddNewPrograms" }, | |
{ FOLDERID_Downloads , L"Downloads" }, | |
{ FOLDERID_PublicDownloads , L"PublicDownloads" }, | |
{ FOLDERID_SavedSearches , L"SavedSearches" }, | |
{ FOLDERID_QuickLaunch , L"QuickLaunch" }, | |
{ FOLDERID_Contacts , L"Contacts" }, | |
{ FOLDERID_SidebarParts , L"SidebarParts" }, | |
{ FOLDERID_SidebarDefaultParts , L"SidebarDefaultParts" }, | |
{ FOLDERID_PublicGameTasks , L"PublicGameTasks" }, | |
{ FOLDERID_GameTasks , L"GameTasks" }, | |
{ FOLDERID_SavedGames , L"SavedGames" }, | |
{ FOLDERID_Games , L"Games" }, | |
{ FOLDERID_SEARCH_MAPI , L"SEARCH_MAPI" }, | |
{ FOLDERID_SEARCH_CSC , L"SEARCH_CSC" }, | |
{ FOLDERID_Links , L"Links" }, | |
{ FOLDERID_UsersFiles , L"UsersFiles" }, | |
{ FOLDERID_UsersLibraries , L"UsersLibraries" }, | |
{ FOLDERID_SearchHome , L"SearchHome" }, | |
{ FOLDERID_OriginalImages , L"OriginalImages" }, | |
{ FOLDERID_DocumentsLibrary , L"DocumentsLibrary" }, | |
{ FOLDERID_MusicLibrary , L"MusicLibrary" }, | |
{ FOLDERID_PicturesLibrary , L"PicturesLibrary" }, | |
{ FOLDERID_VideosLibrary , L"VideosLibrary" }, | |
{ FOLDERID_RecordedTVLibrary , L"RecordedTVLibrary" }, | |
{ FOLDERID_HomeGroup , L"HomeGroup" }, | |
{ FOLDERID_HomeGroupCurrentUser , L"HomeGroupCurrentUser" }, | |
{ FOLDERID_DeviceMetadataStore , L"DeviceMetadataStore" }, | |
{ FOLDERID_Libraries , L"Libraries" }, | |
{ FOLDERID_PublicLibraries , L"PublicLibraries" }, | |
{ FOLDERID_UserPinned , L"UserPinned" }, | |
{ FOLDERID_ImplicitAppShortcuts , L"ImplicitAppShortcuts" }, | |
{ FOLDERID_AccountPictures , L"AccountPictures" }, | |
{ FOLDERID_PublicUserTiles , L"PublicUserTiles" }, | |
{ FOLDERID_AppsFolder , L"AppsFolder" }, | |
{ FOLDERID_StartMenuAllPrograms , L"StartMenuAllPrograms" }, | |
{ FOLDERID_CommonStartMenuPlaces , L"CommonStartMenuPlaces" }, | |
{ FOLDERID_ApplicationShortcuts , L"ApplicationShortcuts" }, | |
{ FOLDERID_RoamingTiles , L"RoamingTiles" }, | |
{ FOLDERID_RoamedTileImages , L"RoamedTileImages" }, | |
{ FOLDERID_Screenshots , L"Screenshots" }, | |
{ FOLDERID_CameraRoll , L"CameraRoll" }, | |
{ FOLDERID_OneDrive , L"OneDrive" }, | |
{ FOLDERID_SkyDriveDocuments , L"SkyDriveDocuments" }, | |
{ FOLDERID_SkyDrivePictures , L"SkyDrivePictures" }, | |
{ FOLDERID_SkyDriveMusic , L"SkyDriveMusic" }, | |
{ FOLDERID_SkyDriveCameraRoll , L"SkyDriveCameraRoll" }, | |
{ FOLDERID_SearchHistory , L"SearchHistory" }, | |
{ FOLDERID_SearchTemplates , L"SearchTemplates" }, | |
{ FOLDERID_CameraRollLibrary , L"CameraRollLibrary" }, | |
{ FOLDERID_SavedPictures , L"SavedPictures" }, | |
{ FOLDERID_SavedPicturesLibrary , L"SavedPicturesLibrary" }, | |
{ FOLDERID_RetailDemo , L"RetailDemo" }, | |
{ FOLDERID_Device , L"Device" }, | |
{ FOLDERID_DevelopmentFiles , L"DevelopmentFiles" }, | |
{ FOLDERID_Objects3D , L"Objects3D" }, | |
{ FOLDERID_AppCaptures , L"AppCaptures" }, | |
{ FOLDERID_LocalDocuments , L"LocalDocuments" }, | |
{ FOLDERID_LocalPictures , L"LocalPictures" }, | |
{ FOLDERID_LocalVideos , L"LocalVideos" }, | |
{ FOLDERID_LocalMusic , L"LocalMusic" }, | |
{ FOLDERID_LocalDownloads , L"LocalDownloads" }, | |
{ FOLDERID_RecordedCalls , L"RecordedCalls" }, | |
{ FOLDERID_AllAppMods , L"AllAppMods" }, | |
{ FOLDERID_CurrentAppMods , L"CurrentAppMods" }, | |
{ FOLDERID_AppDataDesktop , L"AppDataDesktop" }, | |
{ FOLDERID_AppDataDocuments , L"AppDataDocuments" }, | |
{ FOLDERID_AppDataFavorites , L"AppDataFavorites" }, | |
{ FOLDERID_AppDataProgramData , L"AppDataProgramData" }, | |
}; | |
int wmain() { | |
for(const auto& e : entries) { | |
const auto path = getKnownFolderPath(e.knownFolderId); | |
wprintf(L"%-24ls:\"%ls\"\n", e.name, path.c_str()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment