Question. What should we do in the unlikely event that these calls fail? Post a warning or terminate?
Angus
$g++ -o trial trial.C
$ ./trial APPDATA is C:\Documents and Settings\Angus\Application Data COMMON_APPDATA is C:\Documents and Settings\All Users\Application Data PERSONAL is C:\Documents and Settings\Angus\My Documents
$ cat trial.C #include <shlobj.h> #include <iostream> #include <string>
// Needed for MinGW: #ifndef SHGFP_TYPE_CURRENT # define SHGFP_TYPE_CURRENT 0 #endif
std::string const folder_path(int folder_id) { char folder_path[MAX_PATH + 1]; if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0, SHGFP_TYPE_CURRENT, folder_path))) return folder_path; return std::string(); }
std::string const id_as_string(int folder_id) { if (folder_id == CSIDL_APPDATA) return "APPDATA"; if (folder_id == CSIDL_COMMON_APPDATA) return "COMMON_APPDATA"; if (folder_id == CSIDL_PERSONAL) return "PERSONAL"; return std::string(); }
void print_success(int folder_id) { std::string const folder_name = folder_path(folder_id); std::string const id_str = id_as_string(folder_id);
if (folder_name.empty()) std::cout << "Unable to ascertain " << id_str << " folder\n"; else std::cout << id_str << " is " << folder_name << '\n'; }
int main() { print_success(CSIDL_APPDATA); print_success(CSIDL_COMMON_APPDATA); print_success(CSIDL_PERSONAL);
return 0; }