It doesn't crash immediately, the for
loop lists a few file paths then I get this exception:
Unhandled exception at 0x76F05608 in testpath.exe: Microsoft C++ exception: std::filesystem::filesystem_error at memory location 0x0044F8A8.
But it doesn't crash if its CSIDL_DESKTOP
instead of CSIDL_PROGRAM_FILESX86
.
#include <iostream>
#include <filesystem>
#include <shlobj.h>
#pragma comment(lib, "shell32.lib")
namespace fs = std::filesystem;
int main()
{
CHAR programx86[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL, SHGFP_TYPE_CURRENT, programx86);
for (auto& p : fs::recursive_directory_iterator(programx86))
{
std::cout << p.path().u8string() << std::endl;
}
}