开发者

SHGetFolderPath returns path with question marks in it

开发者 https://www.devze.com 2023-02-01 18:13 出处:网络
Our application calls ShGetFolderPath when it runs, to get the My Documents folder. This normally works great. However, for three users - Дмитрий, Jörg and Jörgen (see if you can spot the pat

Our application calls ShGetFolderPath when it runs, to get the My Documents folder. This normally works great. However, for three users - Дмитрий, Jörg and Jörgen (see if you can spot the pattern!) - the call returns some very strange results. For example, for Дмитрий, the call returns:

c:\Users\???????\Documents

I assume there's some sort of character encoding shenanigan going on here, possibly related to Unicode, but I don't have any experience with that sort of thing. How can I get a useful path to the folder (and other related folders) out of windows, without grovelling through registry keys for the information?

In an email to me, Дмитрий ("Dmitry"), told me his "my documents" folder was actually located here:

C:\Users\43D6~1\Documents

So I know there's a way to get a "normal" version of the path out of Windows, I just don't know what it is.

Background: Our application is not unicode-aware, and uses standard "char *" strings. How can w开发者_运维问答e get the "normal" path? I'm not opposed to calling the "unicode" version of the function, then converting it to "normal" text, if that's possible. Converting the application entirely to use unicode is not an option here (we don't have the time).

Thanks.


Go ahead and get the file path in Unicode. Then call GetShortPathNameW to convert to short pathname components. The output shouldn't contain any characters outside of the ASCII range even though it's a Unicode function. You can then truncate each Unicode character back to 8 bits to create a char string.


I'm not opposed to calling the "unicode" version of the function, then converting it to "normal" text, if that's possible.

If you change your call to SHGetFolderPath to SHGetFolderPathW, it will provide you with a string of type LPWSTR, which is a Unicode string. From there, you can use that string with the various Unicode functions that end with W to access the folder or files you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号