想大家请教一个类型转换的问题,谢谢!

bress111 2012-07-19 11:08:51
如果我在程序里面把username和password写死成下面的形式:
char *username="root";
char *password="123";
那么我调用函数登录是可以成功的,但是如果我这样:
先定义:
char *username;
char *password;
然后从编辑框里获取输入的用户名和密码:
CString str_username = m_Connect_UserName.GetBuffer(0);
CString str_password = m_Connect_Password.GetBuffer(0);
MessageBox(str_username);//弹出来"root",说明用户名获取是对的;
MessageBox(str_password);//弹出来"123",说明密码获取是对的;
username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;

这样登录的话就提示用户名和密码错了,怎么回事啊?
网上有人说可能是UNICODE的问题,我不是很懂啊,要怎么处理才行?
...全文
146 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
belindalong 2012-07-19
  • 打赏
  • 举报
回复
username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;
这个是想干什么?比较用户名和密码????
中年風雨 2012-07-19
  • 打赏
  • 举报
回复
你用的是vc200x吧,默认使用unicode编码,如果你debug一下,就会发现str_username是双字节。

用转换函数转成char就可以。


[Quote=引用 4 楼 的回复:]

按照你的方法做的话,错误为:
error C2440: “=”: 无法从“wchar_t *”转换为“char *”引用 2 楼 的回复:
把这两句username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;
改成
username=str_username.GetBuffer(0)……
[/Quote]
  • 打赏
  • 举报
回复
CStringA str_username = m_Connect_UserName.GetBuffer(0);
CStringA str_password = m_Connect_Password.GetBuffer(0);

username = (LPSTR)(LPCSTR)str_username;
password = (LPSTR)(LPCSTR)str_password;


温馨提示:注意str_username str_password作用域
bress111 2012-07-19
  • 打赏
  • 举报
回复
按照你的方法做的话,错误为:
error C2440: “=”: 无法从“wchar_t *”转换为“char *”[Quote=引用 2 楼 的回复:]
把这两句username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;
改成
username=str_username.GetBuffer(0);
str_username.ReleaseBuffer();
password = str_password.GetBuffer(0);
……
[/Quote]
CodeInfo_ 2012-07-19
  • 打赏
  • 举报
回复
看不懂呀,多麻烦,把用户名和密码写入TXT文件,再通过读取TXT文件进行判断
lang14 2012-07-19
  • 打赏
  • 举报
回复
把这两句username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;
改成
username=str_username.GetBuffer(0);
str_username.ReleaseBuffer();
password = str_password.GetBuffer(0);
str_password.ReleaseBuffer();
应该不是Unicode的问题
bress111 2012-07-19
  • 打赏
  • 举报
回复
无人帮忙啊,只有自己顶了
bress111 2012-07-19
  • 打赏
  • 举报
回复
谢谢大家,我自己已经把这个问题解决了!

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧