I am fairly new to Windows development (experienced on the Mac side), and am attempting to use SHGetFolderPath (since it’s compatible with Windows XP) to get the path to the app data folder for the current user. Yet I get an output path of “C” every time, no matter what flag I pass in. I’ve tried both:
CSIDL_APPDATA
and
CSIDL_LOCAL_APPDATA
with and without the CSIDL_FLAG_CREATE flag. Same result every time: “C”. What am I doing wrong here?
#include <windows.h>
#include <shlobj.h> // SHGetFolderPath
#include <stdio.h>
#include <stdlib.h>
//#pragma comment(lib, "shell32.lib")
int main()
{
CHAR path[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path);
if (result != S_OK)
printf("Error: %d\n", result);
else
printf("Path: %s\n" , path);
printf( "String length: %d\n", strlen(path) );
return 0;
}
Documentation for SHGetFolderPath:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx
You are compiling for Unicode but passing an ANSI buffer. Your actual code has a cast that you are not showing us. Either pass a wchar_t buffer and use a matching print routine, or call the ANSI version of the function, SHGetFolderPathA.