免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2906 | 回复: 12
打印 上一主题 下一主题

[C++] 类型转换,问题出在什么地方? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-19 11:08 |只看该作者 |倒序浏览
如果我在程序里面把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的问题,我不是很懂啊,要怎么处理才行?

论坛徽章:
0
2 [报告]
发表于 2012-07-19 11:13 |只看该作者
你在VS工程的配置属性--常规-字符集中选择的是多字节还是UNICODE

论坛徽章:
0
3 [报告]
发表于 2012-07-19 11:27 |只看该作者
fly6 发表于 2012-07-19 11:13
你在VS工程的配置属性--常规-字符集中选择的是多字节还是UNICODE

选的是unicode,我尝试选过多字符集,选后编译可以通过,但是运行的话登录之前的很多操作原本可以成功的都不成功了。

论坛徽章:
0
4 [报告]
发表于 2012-07-19 11:32 |只看该作者
username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;

你这么用风险很高. username和password可能会被传到其它函数里去, 但str_username和str_password已经被析构了. 那两指针指的东西根本就不存在.

论坛徽章:
0
5 [报告]
发表于 2012-07-19 11:34 |只看该作者
hgrany 发表于 2012-07-19 11:32
username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;

谢谢!那怎么转才是安全的,并且效果等同于我写死的那种,然后登录成功呢?

论坛徽章:
0
6 [报告]
发表于 2012-07-19 11:47 |只看该作者
hgrany 发表于 2012-07-19 11:32
username = (LPSTR)(LPCTSTR)str_username;
password = (LPSTR)(LPCTSTR)str_password;


只要函数中不返回它就没事.

论坛徽章:
0
7 [报告]
发表于 2012-07-19 11:53 |只看该作者
@bress111
CString 转 char *      这样也可以吧  char *username= str_username .GetBuffer(str_username.GetLength() +1);
函数传参的时候这样强制转化下 (LPCTSTR)username    看可行不 ?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
8 [报告]
发表于 2012-07-19 11:59 |只看该作者
        CString[color=Red]A[/color] str_username( m_Connect_UserName );
        CString[color=Red]A[/color] str_password( m_Connect_Password );;
        MessageBox[color=Red]A[/color](str_username);//弹出来"root",说明用户名获取是对的;
        MessageBox[color=Red]A[/color](str_password);//弹出来"123",说明密码获取是对的;
        username = (LPSTR)str_username;
        password = (LPSTR)str_password;

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
9 [报告]
发表于 2012-07-19 12:00 |只看该作者
        CStringA str_username( m_Connect_UserName );
        CStringA str_password( m_Connect_Password );;
        MessageBoxA(str_username);//弹出来"root",说明用户名获取是对的;
        MessageBoxA(str_password);//弹出来"123",说明密码获取是对的;
        username = (LPSTR)str_username;
        password = (LPSTR)str_password;

论坛徽章:
0
10 [报告]
发表于 2012-07-19 14:07 |只看该作者
谢谢大家,问题已经解决!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP