std::string.push_back() error in win32

Vijayadithyan .N 121 Reputation points
2022-02-24T12:47:07.03+00:00

There's an error that pops up while calling std::string.push_back in Win32.
More specifically E0167: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
It works fine in regular c++ and older versions, but only in VS2022 and 2019 it shows up
here's the code:

static std::string title;
title.push_back((char)wParam);
SetWindowText(hWnd, title.c_str());

Is there any way to fix this?

Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2022-02-24T12:54:33.183+00:00

    It is more likely that the error message relates to the call to SetWindowText. The mention of LPCWSTR (a Windows data type) indicates that this is happening in a UNICODE build where SetWindowText is resolved to SetWindowTextW for which a LPCWSTR is the second parameter. Consequently, since title.c_str() returns a const char* pointer the error is issued.

    So 1)build for ascii/mbcs or 2) use std::wstring or 3) convert the std::string data to UNICODE.


1 additional answer

Sort by: Most helpful
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2022-02-25T02:17:47.82+00:00

    Hi, @Vijayadithyan .N

    You can also try to use SetWindowTextA

    static std::string title;  
     title.push_back((char)wParam);  
     SetWindowTextA(hWnd, title.c_str());  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.