简体   繁体   English

如何获取%AppData%路径作为std :: string?

[英]How to get %AppData% path as std::string?

I've read that one can use SHGetSpecialFolderPath(); 我已经读到可以使用SHGetSpecialFolderPath(); to get the AppData path. 获取AppData路径。 However, it returns a TCHAR array. 但是,它返回一个TCHAR数组。 I need to have an std::string . 我需要一个std::string

How can it be converted to an std::string ? 如何将其转换为std::string

Update 更新资料

I've read that it is possible to use getenv("APPDATA") , but that it is not available in Windows XP. 我读过可以使用getenv("APPDATA") ,但是在Windows XP中不可用。 I want to support Windows XP - Windows 10. 我想支持Windows XP-Windows 10。

The T type means that SHGetSpecialFolderPath is a pair of functions: T类型表示SHGetSpecialFolderPath是一对函数:

  • SHGetSpecialFolderPathA for Windows ANSI encoded char based text, and SHGetSpecialFolderPathA用于Windows ANSI编码的基于char的文本,以及

  • SHGetSpecialFolderPathW for UTF-16 encoded wchar_t based text, Windows' “Unicode”. SHGetSpecialFolderPathW用于基于UTF-16编码的基于wchar_t的文本,Windows的“ Unicode”。

The ANSI variant is just a wrapper for the Unicode variant, and it can not logically produce a correct path in all cases. ANSI变体只是Unicode变体的包装,在逻辑上不能在所有情况下都产生正确的路径。

But this is what you need to use for char based data. 但这是您需要用于基于char的数据的内容。


An alternative is to use the wide variant of the function, and use whatever machinery that you're comfortable with to convert the wide text result to a byte-oriented char based encoding of your choice, eg UTF-8. 一种替代方法是使用函数的wide变体,并使用您喜欢的任何机器将宽文本结果转换为您选择的基于字节的基于char编码,例如UTF-8。

Note that UTF-8 strings can't be used directly to open files etc. via the Windows API, so this approach involves even more conversion just to use the string. 请注意,UTF-8字符串不能通过Windows API直接用于打开文件等,因此,此方法甚至需要更多的转换才能使用该字符串。


However, I recommend switching over to wide text, in Windows. 但是,我建议在Windows中切换到宽文本。

For this, define the macro symbol UNICODE before including <windows.h> . 为此,请在包含<windows.h>之前定义宏符号UNICODE

That's also the default in a Visual Studio project. 这也是Visual Studio项目中的默认设置。

You should use SHGetSpecialFolderPathA() to have the function deal with ANSI characters explicitly. 您应该使用SHGetSpecialFolderPathA()使函数显式处理ANSI字符。

Then, just convert the array of char to std::string as usual. 然后,像往常一样将char数组转换为std::string

/* to have MinGW declare SHGetSpecialFolderPathA() */
#if !defined(_WIN32_IE) || _WIN32_IE < 0x0400
#undef _WIN32_IE
#define _WIN32_IE 0x0400
#endif

#include <shlobj.h>
#include <string>

std::string getPath(int csidl) {
    char out[MAX_PATH];
    if (SHGetSpecialFolderPathA(NULL, out, csidl, 0)) {
        return out;
    } else {
        return "";
    }
}

https://msdn.microsoft.com/en-gb/library/windows/desktop/dd374131%28v=vs.85%29.aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/dd374131%28v=vs.85%29.aspx

#ifdef UNICODE
    typedef wchar_t TCHAR;
#else
    typedef unsigned char TCHAR;
#endif

Basically you can can convert this array to std::wstring . 基本上,您可以将此数组转换为std::wstring Converting to std::string is straightforward with std::wstring_convert . 使用std::wstring_convert可以直接转换为std::string

http://en.cppreference.com/w/cpp/locale/wstring_convert http://en.cppreference.com/w/cpp/locale/wstring_convert

Typedef String as either std::string or std::wstring depending on your compilation configuration. Typedef String为std :: string或std :: wstring,具体取决于您的编译配置。 The following code might be useful: 以下代码可能有用:

#ifndef UNICODE  
  typedef std::string String; 
#else
  typedef std::wstring String; 
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM