Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHGetFolderPath returns "C" no matter what?

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

like image 729
Vern Jensen Avatar asked Aug 31 '26 21:08

Vern Jensen


1 Answers

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.

like image 102
David Heffernan Avatar answered Sep 02 '26 12:09

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!