+ Reply to Thread
Results 1 to 3 of 3

Thread: SetDlgItemText and string to LPCWSTR?

  1. #1

    SetDlgItemText and string to LPCWSTR?

    Hi All,

    I am having problem with SetWindowText.

    I am using Unicode and one of the input that I am getting from other lib is std::string.

    I want to convert std::string to LPCWSTR?

    How to do this?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Use one of the following methods as per your need:

    Code:
    std::wstring GetWideString(const std::string& strInput)
    {
    	int nInputLength	= (int) strInput.length() + 1 ;
    	int nLength	= MultiByteToWideChar(CP_ACP, 0, strInput.c_str(), nInputLength, 0, 0) ;
    	
    	wchar_t* pbuffer	= new wchar_t[nLength] ;
    	MultiByteToWideChar(CP_ACP, 0, strInput.c_str(), nInputLength, pbuffer, nLength) ;
    	
    	std::wstring wstrReturn(pbuffer) ;
    	
    	delete[] pbuffer ;
    	pbuffer = NULL ;
    
    	return wstrReturn ;
    }
    
    void UseString()
    {
    	std::string str ;
    
    	str = "Hello World!!!" ;
    
    	CString strTemp1(str.c_str()) ;
    
    	CString strTemp2 = CA2T(str.c_str()) ;
    
    	std::wstring wstr = GetWideString(str) ;
    }

  3. #3
    Thanks buddy. It helped me.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts