UC Browser

Thursday, 10 January 2013

Convert LPWSTR to string

C++ Code:

bool LPWSTR2String(std::string& outString, const LPWSTR inLPWSTR, UINT codepage = CP_ACP)
{
    bool retCode = false;
    char* temp = 0;
    int bytesRequired;
    bytesRequired = WideCharToMultiByte(codepage, 0, inLPWSTR, -1, 0, 0, 0, 0);
    if (bytesRequired > 0)
    {
        temp = new char[bytesRequired];
        int rc = WideCharToMultiByte(codepage, 0, inLPWSTR, -1, temp, bytesRequired, 0, 0);
        if (rc != 0)
        {
            temp[bytesRequired-1] = 0;
            outString = temp;
            retCode = true;
        }
    }
    delete [] temp;
    return retCode;
}


2 comments:

  1. How i can convert a string to LPWCSTR?

    ReplyDelete
    Replies
    1. check here http://easytechideas.blogspot.com/2014/11/convert-string-to-lpwstr-c.html

      Delete