std :: string到LPCTSTR [英] std::string to LPCTSTR

查看:495
本文介绍了std :: string到LPCTSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新版本的典型问题如何从 std :: string 转换为 LPCTSTR



阅读不同的SO文章我知道我应该这样做:

  CreateDirectory (path.c_str(),NULL); 

然而编译器给出错误,因为不能从const char *转换为LPCTSTR



我试过:

  CreateDirectory((LPCTSTR)path.c_str(),NULL); 

没有错误!



创建(在正确的位置)被调用:

  D:\\something\\㩄ぜ弲㩄ぜ弲䅓余屓䱆彄湡敤屲䵉ⴱ㠶ⴰⵃㅇ㉜洰⵭就䥄牃獥汵獴촀췍췍췍췍췍췍췍﷍﷽꯽ꮫꮫꮫذوو

这不是我想要的,你可以猜到...



那么我缺少什么?它是与UNICODE / ANSI有关的东西吗?

您的问题在于 LPCTSTR c>根据您的构建是否支持unicode(unicode标志设置或不是)来解析为 wchar_t * char *



要显式调用 char * 版本,请调用 CreateDirectoryA c $ c>。


New version of the typical question of how to convert from std::string to LPCTSTR.

Reading from different SO posts I learnt that I should do this:

CreateDirectory(path.c_str(),NULL);

And still the compiler gives error because cannot convert from const char * to LPCTSTR.

I tried:

CreateDirectory((LPCTSTR)path.c_str(),NULL);

No errors!

Still the directory created (in the correct place) is called:

D:\\something\\㩄ぜ弲久䅓余屓䱆彄湡敤屲䵉ⴱ㠶ⴰⵃㅇ㉜洰⵭就䥄牃獥汵獴촀췍췍췍췍췍췍췍﷍﷽꯽ꮫꮫꮫﺫﻮﻮ

which is not exactly what I wanted, as you can guess...

So what am I missing? Is it something related with UNICODE/ANSI? How can I resolve this?

解决方案

Your problem here is the fact that LPCTSTR is resolved to wchar_t* or char* based on whether your build supports unicode (unicode flag set or not).

To explicitly call the char* version, call CreateDirectoryA().

这篇关于std :: string到LPCTSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆