Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I fix "Severity	Code	Description	Project	File	Line	Suppression State Error (active)	E0167	argument of type "LPSTR" is incompatible with parameter of type "LPCWSTR""?


LPSTR lpsz;
    HWND handle, handle2;
    handle = FindWindowA(NULL, "Calculator");
    GetWindowTextA(handle, lpsz, num);

    //handle2 = FindWindowA(NULL, "Calendar");
    

    //LPCWSTR lpsz2 = lpsz;

    MessageBox(
        NULL,
        lpsz,
        TEXT("Account Details"),
        NULL
    );


What I have tried:

LPSTR lpsz;
    HWND handle, handle2;
    handle = FindWindowA(NULL, "Calculator");
    GetWindowTextA(handle, lpsz, num);

    //handle2 = FindWindowA(NULL, "Calendar");
    

    //LPCWSTR lpsz2 = lpsz;

    MessageBox(
        NULL,
        lpsz,
        TEXT("Account Details"),
        NULL
    );
Posted
Updated 12-Mar-23 14:39pm

It appears that you have UNICODE defined. You call FindWindowA and GetWindowTextA which both take a MBCS character string. Then MessageBox is called and it takes a Unicode OR MBCS string, depending on whether UNICODE is defined. If you call MessageBoxA it will likely compile correctly.
 
Share this answer
 
If you already use = FindWindowA() and GetWindowTextA(), why not MessageBoxA() as well?

If you use C++ you should also think about using std::string and std::wstring.
 
Share this answer
 
You're retrieving the window text with GetWindowTextA, which will return an ANSI string, which is an array of single byte characters. LPSTR stands for "Long Pointer To Ansi String."

The "Long" is a remnant from the old days of DOS, Windows 3.x, and near and far pointers, but is meaningless today in the 32-bit and above world.

Back to your problem, you're trying to pass that single byte Ansi string to a function, MessageBox, that is looking for a LPCWSTR, or "Long Pointer to Constant WIDE Character string". Wide characters are two bytes instead of 1.

So, to make this work easily, you just need to use the wide character LPWSTR for lpsz and the function GetWindowTextW.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900