string怎么转成LPCTSTR?

keary093 2011-12-27 09:27:30
如题,我是在VS2008里面用MFC写的。
我的代码:

string tmp = scanner.GetResult(); // scanner.GetRusult() 返回一个string类型的值
LPCTSTR p = tmp.c_str();
m_ResultEdit.SetWindowText(p);


但是编译时第二条语句出错,说无法从“const char *”转换为“LPCTSTR”。

我改成 LPCTSTR p = (LPCTSTR)tmp.c_str(); 的话,结果就是乱码,请问这是怎么回事?
...全文
1245 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
keary093 2011-12-27
  • 打赏
  • 举报
回复
解决了,是这样的,我把返回的string转成wstring就OK了~结贴~
yyps 2011-12-27
  • 打赏
  • 举报
回复
string是方便的,可以从几乎所有的字符串构造而来,包括CString和char*;

CString次之,可以从基本的一些字符串变量构造而来,包括char*等;

char*没有构造函数,仅可以赋值;

举例:

char* psz = “joise”;

CString cstr( psz );

string str( cstr );

---------------------------
LPCTSTR str = cstr;
黑泡泡选手 2011-12-27
  • 打赏
  • 举报
回复
CString str;
str.Format(_T("%s"), tmp.c_str());
alfwolf 2011-12-27
  • 打赏
  • 举报
回复
楼主,我想你首先需要确定你的工程属性究竟是UNICODE还是Multi-Byte,然后需要确定scanner.GetResult()所得到的究竟是宽字符串还是多字节字符串,然后根据实际情况进行转换,上面的代码都是一些提示性的示例。
keary093 2011-12-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 alfwolf 的回复:]
string tmp = CW2A(scanner.GetResult());
m_ResultEdit.SetWindowText(tmp.c_str());
[/Quote]

也不行,显示:error C2440: “<function-style-cast>”: 无法从“std::string”转换为“ATL::CW2A”
alfwolf 2011-12-27
  • 打赏
  • 举报
回复
string tmp = CW2A(scanner.GetResult());
m_ResultEdit.SetWindowText(tmp.c_str());
keary093 2011-12-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oyljerry 的回复:]
string tmp = (CW2A)scanner.GetResult();
[/Quote]

还是不行,显示:无法从“std::string”转换为“ATL::CW2A”
oyljerry 2011-12-27
  • 打赏
  • 举报
回复
string tmp = (CW2A)scanner.GetResult();
自己实现的字符串类 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };

16,551

社区成员

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

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

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