CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2009
    Posts
    161

    [win32] temp and preference path on windows

    Hi,

    I was searching a place in the windows filesystem to save a preference file. I have found out that onmy system (vista) that place is supposed to be:

    Pref: \Users\MY_ACCOUNT\AppData\Local

    Also, I'd like to know if the following is the correct path to a %temp% directory to save some file for the running session to be delete at the end of the session:

    Temp: \Users\MY_ACCOUNT\AppData\Local\Temp

    Do you think I could retrieve those paths with some API function?

    thanks

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: [win32] temp and preference path on windows

    You could begin by opening a command window and looking at the environment variables. (From the command prompt type "set" - without the quotes). You'll find variables like

    APPDATA
    HOMEPATH
    TEMP
    USERPROFILE

    and so on. You can then use GetEnvironmentVariable to get those values.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [win32] temp and preference path on windows

    Quote Originally Posted by THEARTOFWEB View Post
    ...
    Do you think I could retrieve those paths with some API function?
    Yes!
    SHGetKnownFolderPath Function
    KNOWNFOLDERID
    or
    SHGetFolderPath Function
    CSIDL
    Victor Nijegorodov

  4. #4
    Join Date
    Dec 2009
    Posts
    161

    Re: [win32] temp and preference path on windows

    Ok, I am tring this: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Code:
    char * fullpath;
    HRESULT res = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, 0, fullpath);
    I get this:

    'SHGetKnownFolderPath' : cannot convert parameter 4 from 'char *' to 'PWSTR *'
    how can I cast correctly?

    thanks

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [win32] temp and preference path on windows

    Why do you ignore what MSDN claims?
    Parameter 4 must be of the type PWSTR *, not char *!
    Code:
    PWSTR pName;
    HRESULT res = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, 0, &pName);
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2009
    Posts
    161

    Re: [win32] temp and preference path on windows

    oh well...yeah! Yet, I'd like to normalize that to a regular string or char arrays. Infact if I do like this:

    Code:
    std::cout << *pName << std::endl;
    // or
    std::cout << pName << std::endl;
    I get just numbers :-(

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [win32] temp and preference path on windows

    Quote Originally Posted by THEARTOFWEB View Post
    oh well...yeah! Yet, I'd like to normalize that to a regular string or char arrays. Infact if I do like this:

    Code:
    std::cout << *pName << std::endl;
    // or
    std::cout << pName << std::endl;
    I get just numbers :-(
    You need to start understanding the difference between compiling for UNICODE and MBCS (ANSI). Also in what happens to CString and tchar.h typedef types under the different character sets.

  8. #8
    Join Date
    Dec 2009
    Posts
    161

    Re: [win32] temp and preference path on windows

    Ok, sorted out this way:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <shlobj.h>
    
    int main()
    {
        TCHAR szPath[MAX_PATH] = {0};
    
        HRESULT res = SHGetFolderPath(NULL,
                                      CSIDL_LOCAL_APPDATA|CSIDL_FLAG_DONT_VERIFY,
                                      NULL,
                                      0,
                                      szPath);
    
        std::cout << szPath << std::endl;
    
        
    
        system("pause");
        return EXIT_SUCCESS;
    }
    Now, I was wondering whether there was a way to append a file path to the end of szPath. I mean, is there any function aimed to deal with slashes or is it something I can do on my own??

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [win32] temp and preference path on windows

    Quote Originally Posted by THEARTOFWEB View Post
    Now, I was wondering whether there was a way to append a file path to the end of szPath. I mean, is there any function aimed to deal with slashes or is it something I can do on my own??
    Yep, you can use the CString class which already knows about UNICODE/MBCS.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <shlobj.h>
     
    #include <atlstr.h>
    
    int main()
    {
        CString sPath;
    
        HRESULT res = SHGetFolderPath(
          NULL,
          CSIDL_LOCAL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
          NULL,
          0,
          sPath.GetBuffer( MAX_PATH );
     
        sPath.ReleaseBuffer( );
     
        sPath += _T("\\appendthis");
    
        std::cout << sPath.GetString( ) << std::endl;
     
        // if compiling for UNICODE...
        // std::wcout << sPath.GetString( ) << std::endl;
    
        system("pause");
    
        return EXIT_SUCCESS;
    }

  10. #10
    Join Date
    Dec 2009
    Posts
    161

    Re: [win32] temp and preference path on windows

    Ok, one more thing. Why the following fires an error?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <tchar.h>
    #include <shlobj.h>
    #include <shlwapi.h>
    
    int _tmain()
    {
        TCHAR szPath[MAX_PATH] = {0};
    
        HRESULT res = SHGetFolderPath(NULL,
                                      CSIDL_LOCAL_APPDATA|CSIDL_FLAG_DONT_VERIFY,
                                      NULL,
                                      0,
                                      szPath);
    
        std::cout << szPath << std::endl;
    
        PathAppend(szPath, TEXT("myfile.txt"));
    
        std::cout << szPath << std::endl;
    
        system("pause");
        return EXIT_SUCCESS;
    }
    main.obj : error LNK2019: unresolved external symbol __imp__PathAppendA@8 referenced in function _main

    ???

    thanks

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [win32] temp and preference path on windows

    Quote Originally Posted by THEARTOFWEB View Post
    Ok, one more thing. Why the following fires an error?
    ...
    main.obj : error LNK2019: unresolved external symbol __imp__PathAppendA@8 referenced in function _main

    ???
    Why don't you want to read the PathAppend documentation????
    Please, have a look at the "Function Information section (Header and Import library)
    Victor Nijegorodov

  12. #12
    Join Date
    Dec 2009
    Posts
    161

    Re: [win32] temp and preference path on windows

    yeah, I always miss that :-(

    Code:
    #pragma comment (lib, "shlwapi.lib")
    thanks

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [win32] temp and preference path on windows

    Quote Originally Posted by THEARTOFWEB View Post
    yeah, I always miss that :-(

    Code:
    #pragma comment (lib, "shlwapi.lib")
    thanks
    Or you could just use the CString class and eliminate about 1/3 of your string handling code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured