string 轉換 LPSTR LPCSTR LPCWSTR

LPCSTRLPSTRLPWSTR and LPCWSTR爲指向不同類型字符串的指針。

Call c_str() to get a const char * (LPCSTR) from a std::string.

It’s all in the name:

LPSTR - (long) pointer to string - char *
LPCSTR - (long) pointer to constant string - const char *
LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *
LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t *
LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *

You can ignore the L (long) part of the names – it’s a holdover from 16-bit Windows.

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();

因爲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
}

GitHub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章