错误 C2664:"wsprintfW":无法将参数 1 从"字符 *"转换为"LPWSTR"

error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char *' to 'LPWSTR'

本文关键字:字符 LPWSTR 转换 wsprintfW C2664 错误 参数      更新时间:2023-10-16

我从VC6项目复制了一些代码到vc++2010项目,但代码无法编译。错误是'wsprintfW':无法将参数1从'char *'转换为'LPWSTR',问题代码是:

inline bool RingCtrl::BuildPathAndName(char* pBuf, int bufSize, int8 priority, int idxNumber) const
{
    wsprintf(pBuf, "%s\R%u%06x.DAT", _directory, (int)priority, idxNumber);
    return true;
}

wsprintfwmcommn.h中定义如下:

WINUSERAPI
int
WINAPIV
wsprintfA(
    __out LPSTR,
    __in __format_string LPCSTR,
    ...);
WINUSERAPI
int
WINAPIV
wsprintfW(
    __out LPWSTR,
    __in __format_string LPCWSTR,
    ...);
#ifdef UNICODE
#define wsprintf  wsprintfW
#else
#define wsprintf  wsprintfA
#endif 

您正在构建您的程序与UNICODE定义(默认在vc++ 2010),而它没有在VC6中定义。当UNICODE被定义时,wsprintf的第一个参数是wchar_t*而不是char*,第二个参数是const wchar_t*而不是const char*

简单的解决方案是在RingCtrl::BuildPathAndName中显式地调用wsprintfA而不是wsprintf,但在这种情况下,您将遇到Unicode文件名的问题。你可能会有很多其他类似的错误连接到UNICODE_UNICODE,所以你可能想改变你的项目设置在vc++ 2012:项目属性->通用->字符集->使用多字节字符集。

正确的解决方案是从char*移动到wchar_t*(或者更好地移动到TCHAR*),但这可能需要很多努力。