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++?
c++ file window
add a comment |
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++?
c++ file window
switching your project to a unicode build will probably fix it, alternatively just avoid the wholeTCHARmess, usewchar_tinstead and callSHGetFolderPathWexplicitly.
– 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
add a comment |
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++?
c++ file window
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
c++ file window
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 wholeTCHARmess, usewchar_tinstead and callSHGetFolderPathWexplicitly.
– 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
add a comment |
switching your project to a unicode build will probably fix it, alternatively just avoid the wholeTCHARmess, usewchar_tinstead and callSHGetFolderPathWexplicitly.
– 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
add a comment |
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);
}
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 withPathAppendWyou 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 tocharor::std::string.
– VTT
Nov 12 at 7:25
Thanks. You are very kind.
– auto9817
Nov 13 at 14:10
add a comment |
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);
}
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 withPathAppendWyou 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 tocharor::std::string.
– VTT
Nov 12 at 7:25
Thanks. You are very kind.
– auto9817
Nov 13 at 14:10
add a comment |
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);
}
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 withPathAppendWyou 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 tocharor::std::string.
– VTT
Nov 12 at 7:25
Thanks. You are very kind.
– auto9817
Nov 13 at 14:10
add a comment |
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);
}
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);
}
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 withPathAppendWyou 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 tocharor::std::string.
– VTT
Nov 12 at 7:25
Thanks. You are very kind.
– auto9817
Nov 13 at 14:10
add a comment |
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 withPathAppendWyou 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 tocharor::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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
switching your project to a unicode build will probably fix it, alternatively just avoid the whole
TCHARmess, usewchar_tinstead and callSHGetFolderPathWexplicitly.– 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