Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created April 7, 2021 20:16
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 t-mat/0478bfa8b8a23da70551c63e62308ad7 to your computer and use it in GitHub Desktop.
Save t-mat/0478bfa8b8a23da70551c63e62308ad7 to your computer and use it in GitHub Desktop.
[WIN32] Enumerate all .lnk files in the StartMenu folders.
// Enumerate all .lnk files in the StartMenu folders.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h> // REFKNOWNFOLDERID, SHGetKnownFolderPath
#include <functional>
#include <filesystem>
using EnumStartMenuLnkFilesCallbackFunc = std::function<void(const std::filesystem::path& rootPath, const std::filesystem::path& entryPath)>;
void enumStartMenuLnkFiles(const EnumStartMenuLnkFilesCallbackFunc& callback) {
const auto getKnownFolderPath = [](REFKNOWNFOLDERID id, DWORD flags = 0) -> std::wstring {
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;
};
namespace fs = std::filesystem;
const auto rootPaths = std::vector<fs::path> {
fs::path(getKnownFolderPath(FOLDERID_CommonStartMenu)),
fs::path(getKnownFolderPath(FOLDERID_StartMenu))
};
for(const auto& rootPath : rootPaths) {
for(auto& p: fs::recursive_directory_iterator(rootPath)) {
const auto path = p.path();
if(fs::is_regular_file(p) && path.extension().compare(fs::path(".lnk")) == 0) {
callback(rootPath, path);
}
}
}
}
int main() {
namespace fs = std::filesystem;
setlocale(LC_ALL, "");
enumStartMenuLnkFiles([&](const std::filesystem::path& rootPath, const std::filesystem::path& entryPath) {
const auto relPath = fs::relative(entryPath, rootPath);
wprintf(L"%s\n", relPath.c_str());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment