C++ 从 LPCTSTR 转换为 const char * [英] c++ convert from LPCTSTR to const char *

查看:60
本文介绍了C++ 从 LPCTSTR 转换为 const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MSVC2008 MFC 中有这个问题.我正在使用 unicode.我有一个函数原型:

MyFunction(const char *)

我称之为:

MyfunFunction(LPCTSTR wChar).

<块引用>

错误:无法将参数 1 从LPCTSTR"转换为const char *"

如何解决?

解决方案

由于您使用的是 MFC,您可以轻松地让 CString 进行从 charTCHAR 的自动转换>:

MyFunction(CString(wChar));

无论您的原始字符串是基于 char 还是基于 wchar_t,这都有效.

看来我最初的回答与您所要求的相反.轻松修复:

MyFunction(CStringA(wChar));

CStringACString 的一个版本,它专门包含 char 字符,而不是 TCHAR.还有一个 CStringW 保存 wchar_t.

I have this problem in MSVC2008 MFC. I´m using unicode. I have a function prototype:

MyFunction(const char *)

and I'm calling it:

MyfunFunction(LPCTSTR wChar). 

error:Cannot Convert Parameter 1 From 'LPCTSTR' to 'const char *'

How to resolve it?

解决方案

Since you're using MFC, you can easily let CString do an automatic conversion from char to TCHAR:

MyFunction(CString(wChar));

This works whether your original string is char or wchar_t based.

Edit: It seems my original answer was opposite of what you asked for. Easily fixed:

MyFunction(CStringA(wChar));

CStringA is a version of CString that specifically contains char characters, not TCHAR. There's also a CStringW which holds wchar_t.

这篇关于C++ 从 LPCTSTR 转换为 const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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