Robust ways to get App Data Folder Path for Non English Window User











up vote
0
down vote

favorite












I was using this code well to get the App Data Folder path for my C++ application.



    char* actFilePath = NULL;
TCHAR szPath[MAX_PATH];


if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, szPath)))
{
PathAppend(szPath, _T("\MyFile.txt"));
actFilePath = wchar_to_string(szPath);
}


When I run the this code over some of Non English Window 8 or Window 10 OS, this code fails (actFilePath is just null). I found that the code fails due the non English User name in the folder path like débarquer Matyáš or 姓 名 as you can see from the path below:



C:Usersdébarquer MatyášAppDataLocal
C:Users姓 名AppDataLocal


What are more robust approach and less prone to error just in case some user name is written in their native language even including Chinese or Japanese or European, etc. Working code example will be really appreciated.



Kind regards.



=====================================================================



Updated to the answer from VTT on 12 Nov 2018



I created this code following the answer from VTT.
This code compiles fine. However, the returned folder path is behaving unexpected. Sometimes, it gives me the correct path but sometimes, it returns unreadable file path. Something like this. See attached link for some strange characters. Some impression I am getting is this code is unstable.



https://ibb.co/goLzxq



    wchar_t* actFilePath = NULL;

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &actFilePath)))
{
PathAppendW(actFilePath, L"\MyFile.txt");
}


I have followed some advice from this answer too here.



How do I convert PWSTR to string in C++?










share|improve this question
























  • switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
    – Alan Birtles
    Nov 11 at 7:40










  • I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
    – auto9817
    Nov 11 at 16:01















up vote
0
down vote

favorite












I was using this code well to get the App Data Folder path for my C++ application.



    char* actFilePath = NULL;
TCHAR szPath[MAX_PATH];


if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, szPath)))
{
PathAppend(szPath, _T("\MyFile.txt"));
actFilePath = wchar_to_string(szPath);
}


When I run the this code over some of Non English Window 8 or Window 10 OS, this code fails (actFilePath is just null). I found that the code fails due the non English User name in the folder path like débarquer Matyáš or 姓 名 as you can see from the path below:



C:Usersdébarquer MatyášAppDataLocal
C:Users姓 名AppDataLocal


What are more robust approach and less prone to error just in case some user name is written in their native language even including Chinese or Japanese or European, etc. Working code example will be really appreciated.



Kind regards.



=====================================================================



Updated to the answer from VTT on 12 Nov 2018



I created this code following the answer from VTT.
This code compiles fine. However, the returned folder path is behaving unexpected. Sometimes, it gives me the correct path but sometimes, it returns unreadable file path. Something like this. See attached link for some strange characters. Some impression I am getting is this code is unstable.



https://ibb.co/goLzxq



    wchar_t* actFilePath = NULL;

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &actFilePath)))
{
PathAppendW(actFilePath, L"\MyFile.txt");
}


I have followed some advice from this answer too here.



How do I convert PWSTR to string in C++?










share|improve this question
























  • switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
    – Alan Birtles
    Nov 11 at 7:40










  • I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
    – auto9817
    Nov 11 at 16:01













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was using this code well to get the App Data Folder path for my C++ application.



    char* actFilePath = NULL;
TCHAR szPath[MAX_PATH];


if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, szPath)))
{
PathAppend(szPath, _T("\MyFile.txt"));
actFilePath = wchar_to_string(szPath);
}


When I run the this code over some of Non English Window 8 or Window 10 OS, this code fails (actFilePath is just null). I found that the code fails due the non English User name in the folder path like débarquer Matyáš or 姓 名 as you can see from the path below:



C:Usersdébarquer MatyášAppDataLocal
C:Users姓 名AppDataLocal


What are more robust approach and less prone to error just in case some user name is written in their native language even including Chinese or Japanese or European, etc. Working code example will be really appreciated.



Kind regards.



=====================================================================



Updated to the answer from VTT on 12 Nov 2018



I created this code following the answer from VTT.
This code compiles fine. However, the returned folder path is behaving unexpected. Sometimes, it gives me the correct path but sometimes, it returns unreadable file path. Something like this. See attached link for some strange characters. Some impression I am getting is this code is unstable.



https://ibb.co/goLzxq



    wchar_t* actFilePath = NULL;

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &actFilePath)))
{
PathAppendW(actFilePath, L"\MyFile.txt");
}


I have followed some advice from this answer too here.



How do I convert PWSTR to string in C++?










share|improve this question















I was using this code well to get the App Data Folder path for my C++ application.



    char* actFilePath = NULL;
TCHAR szPath[MAX_PATH];


if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, szPath)))
{
PathAppend(szPath, _T("\MyFile.txt"));
actFilePath = wchar_to_string(szPath);
}


When I run the this code over some of Non English Window 8 or Window 10 OS, this code fails (actFilePath is just null). I found that the code fails due the non English User name in the folder path like débarquer Matyáš or 姓 名 as you can see from the path below:



C:Usersdébarquer MatyášAppDataLocal
C:Users姓 名AppDataLocal


What are more robust approach and less prone to error just in case some user name is written in their native language even including Chinese or Japanese or European, etc. Working code example will be really appreciated.



Kind regards.



=====================================================================



Updated to the answer from VTT on 12 Nov 2018



I created this code following the answer from VTT.
This code compiles fine. However, the returned folder path is behaving unexpected. Sometimes, it gives me the correct path but sometimes, it returns unreadable file path. Something like this. See attached link for some strange characters. Some impression I am getting is this code is unstable.



https://ibb.co/goLzxq



    wchar_t* actFilePath = NULL;

if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &actFilePath)))
{
PathAppendW(actFilePath, L"\MyFile.txt");
}


I have followed some advice from this answer too here.



How do I convert PWSTR to string in C++?







c++ file window






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:51

























asked Nov 11 at 7:14









auto9817

2818




2818












  • switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
    – Alan Birtles
    Nov 11 at 7:40










  • I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
    – auto9817
    Nov 11 at 16:01


















  • switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
    – Alan Birtles
    Nov 11 at 7:40










  • I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
    – auto9817
    Nov 11 at 16:01
















switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
– Alan Birtles
Nov 11 at 7:40




switching your project to a unicode build will probably fix it, alternatively just avoid the whole TCHAR mess, use wchar_t instead and call SHGetFolderPathW explicitly.
– Alan Birtles
Nov 11 at 7:40












I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
– auto9817
Nov 11 at 16:01




I checked my compiler and in fact it did compiled with Unicode setting. So your first suggestion does not work. I guess it is sensitive to window OS.
– auto9817
Nov 11 at 16:01












1 Answer
1






active

oldest

votes

















up vote
1
down vote













SHGetFolderPath is deprecated. You should use SHGetKnownFolderPath instead. Note that this new function only has wide char version so it works with Unicode paths properly.



PWSTR psz_path{};
auto const hr
{
::SHGetKnownFolderPath
(
FOLDERID_LocalAppData
, KF_FLAG_DEFAULT
, HANDLE{}
, ::std::addressof(psz_path)
)
};
if(SUCCEEDED(hr))
{
assert(psz_path);
// do something with path...
::CoTaskMemFree(psz_path);
}





share|improve this answer





















  • Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
    – auto9817
    Nov 12 at 6:50












  • You can check the strange behaviour here. ibb.co/goLzxq
    – auto9817
    Nov 12 at 6:51










  • @auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
    – VTT
    Nov 12 at 7:25












  • Thanks. You are very kind.
    – auto9817
    Nov 13 at 14:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246607%2frobust-ways-to-get-app-data-folder-path-for-non-english-window-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













SHGetFolderPath is deprecated. You should use SHGetKnownFolderPath instead. Note that this new function only has wide char version so it works with Unicode paths properly.



PWSTR psz_path{};
auto const hr
{
::SHGetKnownFolderPath
(
FOLDERID_LocalAppData
, KF_FLAG_DEFAULT
, HANDLE{}
, ::std::addressof(psz_path)
)
};
if(SUCCEEDED(hr))
{
assert(psz_path);
// do something with path...
::CoTaskMemFree(psz_path);
}





share|improve this answer





















  • Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
    – auto9817
    Nov 12 at 6:50












  • You can check the strange behaviour here. ibb.co/goLzxq
    – auto9817
    Nov 12 at 6:51










  • @auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
    – VTT
    Nov 12 at 7:25












  • Thanks. You are very kind.
    – auto9817
    Nov 13 at 14:10















up vote
1
down vote













SHGetFolderPath is deprecated. You should use SHGetKnownFolderPath instead. Note that this new function only has wide char version so it works with Unicode paths properly.



PWSTR psz_path{};
auto const hr
{
::SHGetKnownFolderPath
(
FOLDERID_LocalAppData
, KF_FLAG_DEFAULT
, HANDLE{}
, ::std::addressof(psz_path)
)
};
if(SUCCEEDED(hr))
{
assert(psz_path);
// do something with path...
::CoTaskMemFree(psz_path);
}





share|improve this answer





















  • Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
    – auto9817
    Nov 12 at 6:50












  • You can check the strange behaviour here. ibb.co/goLzxq
    – auto9817
    Nov 12 at 6:51










  • @auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
    – VTT
    Nov 12 at 7:25












  • Thanks. You are very kind.
    – auto9817
    Nov 13 at 14:10













up vote
1
down vote










up vote
1
down vote









SHGetFolderPath is deprecated. You should use SHGetKnownFolderPath instead. Note that this new function only has wide char version so it works with Unicode paths properly.



PWSTR psz_path{};
auto const hr
{
::SHGetKnownFolderPath
(
FOLDERID_LocalAppData
, KF_FLAG_DEFAULT
, HANDLE{}
, ::std::addressof(psz_path)
)
};
if(SUCCEEDED(hr))
{
assert(psz_path);
// do something with path...
::CoTaskMemFree(psz_path);
}





share|improve this answer












SHGetFolderPath is deprecated. You should use SHGetKnownFolderPath instead. Note that this new function only has wide char version so it works with Unicode paths properly.



PWSTR psz_path{};
auto const hr
{
::SHGetKnownFolderPath
(
FOLDERID_LocalAppData
, KF_FLAG_DEFAULT
, HANDLE{}
, ::std::addressof(psz_path)
)
};
if(SUCCEEDED(hr))
{
assert(psz_path);
// do something with path...
::CoTaskMemFree(psz_path);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 8:15









VTT

23.4k42345




23.4k42345












  • Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
    – auto9817
    Nov 12 at 6:50












  • You can check the strange behaviour here. ibb.co/goLzxq
    – auto9817
    Nov 12 at 6:51










  • @auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
    – VTT
    Nov 12 at 7:25












  • Thanks. You are very kind.
    – auto9817
    Nov 13 at 14:10


















  • Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
    – auto9817
    Nov 12 at 6:50












  • You can check the strange behaviour here. ibb.co/goLzxq
    – auto9817
    Nov 12 at 6:51










  • @auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
    – VTT
    Nov 12 at 7:25












  • Thanks. You are very kind.
    – auto9817
    Nov 13 at 14:10
















Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
– auto9817
Nov 12 at 6:50






Thanks for suggestion. As I have updated. The code I have created is behaving strange giving me some path contained Unicode,etc. Can you suggest how to append the app data folder path with file name for further use with wchar_t* filename.
– auto9817
Nov 12 at 6:50














You can check the strange behaviour here. ibb.co/goLzxq
– auto9817
Nov 12 at 6:51




You can check the strange behaviour here. ibb.co/goLzxq
– auto9817
Nov 12 at 6:51












@auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
– VTT
Nov 12 at 7:25






@auto9817 Pointer value returned by this function will point to the buffer large enough to hold only the folder path, so by appending something to it with PathAppendW you will get a buffer overrun and memory corruption. You should allocate separate buffer large enough to hold full file path and then append folder path and file name to that buffer. Actually just use ::std::wstring. And you should not convert it path to char or ::std::string.
– VTT
Nov 12 at 7:25














Thanks. You are very kind.
– auto9817
Nov 13 at 14:10




Thanks. You are very kind.
– auto9817
Nov 13 at 14:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246607%2frobust-ways-to-get-app-data-folder-path-for-non-english-window-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word