get application data folder path

This is a discussion on get application data folder path within the C++ Programming forums, part of the General Programming Boards category; Hi everybody! I need to find the path to the application data folder. So I use the function SHGetFolderPath with ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    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

    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();
        #else
        return scz;
        #endif
    }
     
    int _tmain(int argc, _TCHAR* argv[])
    {
    	cout << "Get Application Data"<< endl;
    	string path="";
    	TCHAR szPath[MAX_PATH];
    	if(SUCCEEDED(SHGetFolderPath(NULL, 
                                 CSIDL_APPDATA, 
                                 NULL, 
                                 0, 
                                 szPath))) 
    	{
    		path = TcharsToString(szPath);
    	}
    	cout << path << endl;
    	system("PAUSE");
    	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
    Posts
    903
    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
    Posts
    2
    My exact output is

    0012FD24

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    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
    Location
    Banks of the River Styx
    Posts
    902
    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-07-2007, 11:24 PM
  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 Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21