Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

c++ - How do I convert from LPCTSTR to std::string?

I have an LPCTSTR and want to call a function that takes a std::string parameter.

What conversion do I need to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Tip of the iceberg

LPCTSTR can be either a single-byte or a multibyte string (depends on the UNICODE constant being defined during compilation or not), while std::string's users (including your function) normally use it to hold a single-byte string.

You 'd need two conversions: one for LPCSTR (non-UNICODE build) and one for LPCWSTR (UNICODE build). The first one is simple:

std::string convert(LPCSTR str) {
    return std::string(str);
}

The second one needs its input parameter to be converted to another encoding first with WideCharToMultiByte. Do not be alarmed by the name, the result can be a single-byte char string; that depends on the CodePage parameter. You will have to use a codepage for a single-byte encoding, such as CP_ACP.

Update: WideCharToMultiByte example

Be aware that accurately converting to a single-byte encoding is technically impossible if the input string contains characters not existing in the target encoding's code page. Since you mention it's going to be for filesystem functions, if the file path contains such characters the conversion will not be 100% accurate and the subsequent function calls will fail.

std::string MBFromW(LPCWSTR pwsz, UINT cp) {
    int cch = WideCharToMultiByte(cp, 0, pwsz, -1, 0, 0, NULL, NULL);

    char* psz = new char[cch];

    WideCharToMultiByte(cp, 0, pwsz, -1, psz, cch, NULL, NULL);

    std::string st(psz);
    delete[] psz;

   return st;
}

Caveat emptor: The example above is from some code I had lying around and is not production-grade quality. The one immediately obvious flaw is that it is not exception-safe. It might also kill all the nice purple unicorns. Use it only as an example.

The full encoding hell

The naked truth is that std::string can be used for multibyte encodings (such as UTF8) just fine -- you can even use it to hold wide-char strings, since it's just a binary-safe array of bytes at heart.

The problem is that the STL functions that apply to std::string expect its contents to be in a single-byte encoding, and they won't produce correct results if this is not true.

By extension, we don't know what your function that takes an std::string parameter expects -- it might expect a string encoded in UTF-8. But "by convention", I 'm assuming it also wants a single-byte-encoded string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...