编译ERROR C2440: 无法从“const char [47]”转换为“LPCTSTR”

来源:互联网 发布:java memorycache 编辑:程序博客网 时间:2024/05/29 03:18

error C2440: “=”: 无法从“const char [47]”转换为“LPCTSTR”
作者:ImMcss 欢迎转载,请以超链接形式保留原文地址。

在开发过程中经常会遇到使用LPCTSTR型作为输入参数的API函数。直接传字符串会出来"C2440"编译错误。
例如代码:
LPCTSTR   StrKey="HARDWARE//DESCRIPTION//System//CentralProcessor//0";
会出现下面的编译错误:
error C2440: “=”: 无法从“const char [47]”转换为“LPCTSTR”。
看看LPCTSTR在winnt.h中的定义
        typedef LPCWSTR PCTSTR, LPCTSTR;
        typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;
        typedef wchar_t WCHAR;
从定义可以看出LPCTSTR就是WCHAR指针,因为wchar_t定义的是宽字符集,所以做如下修正:
LPCTSTR   StrKey=(WCHAR *)_T("HARDWARE//DESCRIPTION//System//CentralProcessor//0");


原创粉丝点击