C++ visual studio error C2664

2019-09-06 14:07发布

When I use this code

if (GetKeyNameText(Key << 16, NameBuffer, 127))
{
    KeyName = NameBuffer;
    GoodKeyName = true;
}

I get the following error

C2664 'int GetKeyNameTextW(LONG,LPWSTR,int)': cannot convert argument 2 from 'char [128]' to 'LPWSTR'

The NameBuffer says this:

Error: argument of type "char*" is incompatible with parameter of type "LPWSTR"

Any tips?

标签: c++ c2664
1条回答
在下西门庆
2楼-- · 2019-09-06 14:23

You have UNICODE defined, which means all your functions and TCHAR and LPTSTR are defaulting to wide characters (wchar_t).

That means you can't use a narrow-character string (using char) without special care.

There is an easy solution, and that's to explicitly call the narrow-character version of the function: GetKeyNameTextA.

Another solution is to stop using char and change to TCHAR and related types, and use the T macro for string literals.

You might want to read more about UNICODE in the Windows API.

查看更多
登录 后发表回答