如何将std :: string转换为LPCSTR?

Cut*_*ute 109 c++ windows string

我怎样才能转换std::stringLPCSTR?另外,我如何将a转换std::stringLPWSTR

我对这些LPCSTR LPSTR LPWSTR和我完全感到困惑LPCWSTR.

LPWSTRLPCWSTR一样吗?

Nic*_*yer 149

打电话从a c_str()获得一个const char *(LPCSTR)std::string.

这一切都在名称中:

LPSTR - (长)指向字符串的指针 - char *

LPCSTR - (long)指向常量字符串的指针 - const char *

LPWSTR - (long)指向Unicode(宽)字符串的指针 - wchar_t *

LPCWSTR - (long)指向常量Unicode(宽)字符串的指针 - const wchar_t *

LPTSTR - (长)指向TCHAR的指针(如果定义了UNICODE,则为Unicode,否则为ANSI)字符串 - TCHAR *

LPCTSTR - (long)指向常量TCHAR字符串的指针 - const TCHAR *

您可以忽略名称的L(长)部分 - 它是16位Windows的保留.


Lou*_*nco 112

str.c_str()给你一个const char *,它是一个LPCSTR(常量0字符串的长指针) - 意味着它是一个指向终止字符串的指针. W表示宽字符串(由wchar_t代替char)组成.

  • 轻微挑剔点:在x64上,LPCSTR将是一个指向(常量)以null结尾的字符串的64位指针. (5认同)

CB *_*ley 32

这些是Microsoft定义的typedef,对应于:

LPCSTR:指向null终止的const字符串的指针 char

LPSTR:指向null终止的char字符串的指针char(通常传递一个缓冲区并用作'output'参数)

LPCWSTR:指向以null结尾的const字符串的指针 wchar_t

LPWSTR:指向以null结尾的字符串的指针wchar_t(通常传递缓冲区并用作'输出'参数)

将"转换"a转换std::string为LPCSTR取决于确切的上下文,但通常调用.c_str()就足够了.

这有效.

void TakesString(LPCSTR param);

void f(const std::string& param)
{
    TakesString(param.c_str());
}
Run Code Online (Sandbox Code Playgroud)

请注意,您不应该尝试这样做.

LPCSTR GetString()
{
    std::string tmp("temporary");
    return tmp.c_str();
}
Run Code Online (Sandbox Code Playgroud)

返回的缓冲区由实例.c_str()拥有,std::string并且只有在下一次修改或销毁字符串之前才有效.

将a转换std::string为a LPWSTR更复杂.婉婷的LPWSTR暗示,你需要一个缓冲修改的,你还需要确保您了解什么编码字符std::string使用.如果std::string包含使用系统默认编码的字符串(假设这里是windows),那么您可以找到所需宽字符缓​​冲区的长度并使用MultiByteToWideChar(Win32 API函数)执行转码.

例如

void f(const std:string& instr)
{
    // Assumes std::string is encoded in the current Windows ANSI codepage
    int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);

    if (bufferlen == 0)
    {
        // Something went wrong. Perhaps, check GetLastError() and log.
        return;
    }

    // Allocate new LPWSTR - must deallocate it later
    LPWSTR widestr = new WCHAR[bufferlen + 1];

    ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);

    // Ensure wide string is null terminated
    widestr[bufferlen] = 0;

    // Do something with widestr

    delete[] widestr;
}
Run Code Online (Sandbox Code Playgroud)


Kir*_*sky 18

使用LPWSTR您可以更改指向的字符串的内容.使用LPCWSTR您无法更改指向的字符串的内容.

std::string s = SOME_STRING;
// get temporary LPSTR (not really safe)
LPSTR pst = &s[0];
// get temporary LPCSTR (pretty safe)
LPCSTR pcstr = s.c_str();
// convert to std::wstring
std::wstring ws; 
ws.assign( s.begin(), s.end() );
// get temporary LPWSTR (not really safe)
LPWSTR pwst = &ws[0];
// get temporary LPCWSTR (pretty safe)
LPCWSTR pcwstr = ws.c_str();
Run Code Online (Sandbox Code Playgroud)

LPWSTR只是指向原始字符串的指针.您不应该使用上面的示例从函数返回它.为了不是临时的,LPWSTR你应该在堆上创建一个原始字符串的副本.检查以下示例:

LPWSTR ConvertToLPWSTR( const std::string& s )
{
  LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end
  copy( s.begin(), s.end(), ws );
  ws[s.size()] = 0; // zero at the end
  return ws;
}

void f()
{
  std::string s = SOME_STRING;
  LPWSTR ws = ConvertToLPWSTR( s );

  // some actions

  delete[] ws; // caller responsible for deletion
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*dad 5

转换很简单:

std::string myString;

LPCSTR lpMyString = myString.c_str();
Run Code Online (Sandbox Code Playgroud)

这里需要注意的一件事是 c_str 不会返回 myString 的副本,而只是返回指向 std::string 包装的字符串的指针。如果您想要/需要一份副本,您需要使用 strcpy 自己制作一份。


Joe*_*oel 5

MultiByteToWideCharCharles Bailey 给出的答案是正确的。因为LPCWSTR只是 for 的 typedef const WCHAR*widestr在示例代码中,可以在LPWSTR预期 a 或LPCWSTR预期a 的任何地方使用。

一个小的调整是使用std::vector<WCHAR>而不是手动管理的数组:

// using vector, buffer is deallocated when function ends
std::vector<WCHAR> widestr(bufferlen + 1);

::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), &widestr[0], bufferlen);

// Ensure wide string is null terminated
widestr[bufferlen] = 0;

// no need to delete; handled by vector
Run Code Online (Sandbox Code Playgroud)

此外,如果您需要使用宽字符串开始,您可以使用std::wstring代替std::string. 如果要使用 WindowsTCHAR类型,可以使用std::basic_string<TCHAR>. 从std::wstringtoLPCWSTR或 from std::basic_string<TCHAR>to转换LPCTSTR只是调用c_str. 当您在 ANSI 和 UTF-16 字符之间进行更改时MultiByteToWideChar(及其反字符WideCharToMultiByte)就会出现。