Thread: get application data folder path

  1. #1
    Registered User
    Join Date
    Jun 2009

    get application data folder path

    Hi everybody!

    I need to find the path to the application data folder. So I use the function SHGetFolderPath with the CSID: CSIDL_APPDATA

    Here is my code

    #include <iostream>
    #include <sstream>
    #include <Windows.h> // for MAX_PATH
    #include <shlobj.h> // for getFolderPath function
    using namespace std;
    // Convert TCHAR to string
    std::string TcharsToString(TCHAR const * scz)
        #ifdef UNICODE
        std::ostringstream ossA;
        ossA << scz;
        return ossA.str();
        return scz;
    int _tmain(int argc, _TCHAR* argv[])
    	cout << "Get Application Data"<< endl;
    	string path="";
    	TCHAR szPath[MAX_PATH];
    		path = TcharsToString(szPath);
    	cout << path << endl;
    	return 0;
    The problemm is that the string "path" contains series of characters with numbers like "0012FD24"

    But normally, I would rather have a string like "C:\documents and settings\user\... , no?

    Is there a problem with the conversion TCHAR -> String?

    So, How could I have the path in return?

    Thanks in advance for your answers

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Does SHGetFolderPath() take care of null-termination for the TCHAR*? Is ostringstream overloaded for TCHAR*? I'm on a linux system right now, so I can't poke around.

    What is you exact output?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jun 2009
    My exact output is


  4. #4
    Registered User
    Join Date
    Apr 2007
    SHGetFolderPath is a Win32 api : you can ask on Win32 api group for the official code (MS internal...)

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    I'm fairly sure the OP is calling SHGetFolderPath correctly. However, I highly doubt that std:stringstream is overloaded for TCHAR * -- how could it be, since it's templated on char? (And TCHAR != char if Unicode is defined.)

    OP: Why do you want to convert to a char */std::string? What character set do you expect the output in the char * to be?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

Tags for this Thread