|
发表于 2014-7-26 08:06:03
|
显示全部楼层
没有发出错误提示。不知道我们编译出现的错误是否一致首先我项目属性设置的是使用ASCII字符集
LPWSTR szString = TEXT("hello");
error C2440: 'initializing' : cannot convert from 'char [6]' to 'unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
类型不一致
定义的是宽字符的变量。项目属性设置的是使用ASCII字符集,TEXT因此会把hello转换成ASCII的形式。于是无法赋值
修改成LPWSTR szString = L"hello";把hello转换成宽字符。或者把项目属性设置为使用UNICODE字符集,这里选择前者
MessageBox(NULL,szString,TEXT("LPSTR"),MB_OK);
error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'unsigned short *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
同样的项目属性设置的是使用ASCII字符集,MessageBox=MessageBoxA,也就是使用ASCII字符串作为参数
这里却用了szString宽字符
索性干脆这样改MessageBoxW(NULL,szString,L"LPSTR",MB_OK);
CopyMemory(lpString,szString,lstrlen(szString)+1);
error C2664: 'lstrlenA' : cannot convert parameter 1 from 'unsigned short *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
lstrlen因为项目属性设置的是使用ASCII字符集,所以lstrlen=lstrlenA无法获取szString大小,那么
CopyMemory(lpString,szString,lstrlenW(szString)+1);
不过上面几乎是打补丁式的做法。干脆这样
- #include <Windows.h>
- #include <iostream>
- int main()
- {
- LPSTR szString = "hello";//直接改
- TCHAR lpString[10];
- MessageBox(NULL,szString,TEXT("LPSTR"),MB_OK);
- CopyMemory(lpString,szString,lstrlen(szString)+1);
- MessageBox(NULL,lpString,TEXT("CHAR"),MB_OK);
-
- return 0;
- }
复制代码
|
|