String--cannot convert from 'const char *' to 'LPTSTR'错误
这种错误,很多情况下是类型不匹配
LPTSTR表示为指向常量TCHAR字符串的长指针
TCHAR可以是wchar_t或char,基于项目是多字节还是宽字节版本。
看下面的代码,代码来源:Example: Open a File for Reading
DisplayError(TEXT("CreateFile"));
void DisplayError(LPTSTR lpszFunction)
// Routine Description:
// Retrieve and output the system error message for the last-error code
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL );
lpDisplayBuf =
(LPVOID)LocalAlloc( LMEM_ZEROINIT,
( lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)lpszFunction)
+ 40) // account for format string
* sizeof(TCHAR) );
if (FAILED( StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error code %d as follows:\n%s"),
lpszFunction,
dw,
lpMsgBuf)))
{
printf("FATAL ERROR: Unable to output error code.\n");
}
_tprintf(TEXT("ERROR: %s\n"), (LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
编译的时候,会出现标题中的错误,
错误处:
DisplayError(TEXT("CreateFile"));
并且在调试时,我们会发现出现????这样的错误,检查参数时会发现一堆奇怪的文字。
原因是在宽字节版本下,我们传入的是单个字节,而wchar_t*指向它们的字符将期望每个字符都是2字节,所以就会出现????
我们可以这样简单的转换,宽字节版本:
DisplayError((LPTSTR)L"CreateFile");
多字节版本:
DisplayError((LPTSTR)"CreateFile");
但是我们最好要停止使用TCHAR类型,取而代之,使用mbstowcs()或MultiByteToWideChar()将char字符串转换为utf16。或始终使用wchar_t std :: wstring
多字节版本:
std::string str = "CreateFile";
const char* lp = str.c_str(); // or
LPCSTR lp = str.c_str();
宽字节版本:
std::wstring wstr = L"CreateFile";
const wchar_t* lp = wstr.c_str() // or
LPCWSTR lp = wstr.c_str();
另附mbstowcs的用法:
std::string str = "CreateFile"; //多字节
wchar_t wstr[11];
mbstowcs(wstr, str.c_str(), str.size()+1);
DisplayError(wstr); //宽字节版本
String--cannot convert from 'const char *' to 'LPTSTR'错误的更多相关文章
- 【转】char*,const char*和string的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- string,const char*,char*之间的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- C++ char*,const char*,string的相互转换
1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string ...
- [转载] C++ string, const char*, char* 之间互相转换
1, string转const char* 类型 string str = "abcdef"; const char* con_str = string.c_str() 2, co ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- char*,const char*和string的相互转换
好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1 ...
- CString、string、const char*的相互转换
环境:vs2010 1.CString转string //第一种方式: CString str = _T("CSDN"); USES_CONVERSION; std::string ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- 【转载】char*,const char*和string 三者转换
本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678 const char* 和string 转换 const char*转换 ...
- 转载:string、const char*、 char* 、char[]相互转换
本文转自:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 一:转化总结形式如下: 使用时,要对源格式和目标格式进行初始化 ...
随机推荐
- [转帖]TiFlash DeltaTree 存储引擎设计及实现分析 - Part 1
https://tidb.net/book/book-rush/features/tiflash-code/tiflash-deltatree TiFlash 是 TiDB 的分析引擎,是 TiDB ...
- 【转帖】Dockerfile文件指令介绍
https://blog.whsir.com/post-5327.html Dockerfile其实就是一个文本文件,这个文本文件名称叫Dockerfile,里面包含了一些指令(可以理解成多个指令集合 ...
- [转帖]CentOS7完美升级gcc版本方法
https://blog.whsir.com/post-4975.html 在某些应用场景中,需要特定的gcc版本支持,但是轻易不要去编译gcc.不要去编译gcc.不要去编译gcc,我这里推荐使用红帽 ...
- [转帖]讨论在 Linux Control Groups 中运行 Java 应用程序的暂停问题原创
https://heapdump.cn/article/1930426 说明 本篇原文来自 LinkedIn 的 Zhenyun Zhuang,原文:Application Pauses When R ...
- [转帖]计算机体系结构-cache高速缓存
https://zhuanlan.zhihu.com/p/482651908 本文主要介绍了cache的基本常识.基本组成方式.写入方法和替换策略,在基本组成方式和替换策略两节给出了较为详细的硬件实现 ...
- [翻译]-hugePage的简要说明--部分内容
hugePage的简要说明 本篇文档的主旨给linux内核支持的大页内存做一个简要的概述. 大页内存的实现是建立在大多数现代架构所都支持的多级页大小的特性之上的. 举例: x86架构下大部署CPU 的 ...
- 简单监控Tomcat连接池大小的命令以及其他简单命令
while true ; do date && echo "当前数据库连接池大小为:" $(jmap -histo `jps |grep caf |awk '{pr ...
- Mysql到TiDB迁移,双写数据库兜底方案
作者:京东零售 石磊 TiDB 作为开源 NewSQL 数据库的典型代表之一,同样支持 SQL,支持事务 ACID 特性.在通讯协议上,TiDB 选择与 MySQL 完全兼容,并尽可能兼容 MySQL ...
- PHP伪协议与文件包含漏洞
https://www.cnblogs.com/weak-chicken/p/12275806.html 1 file:// - 访问本地文件系统 2 http:// - 访问 HTTP(s) 网址 ...
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
往期项目回顾: Paddlenlp之UIE模型实战实体抽取任务[打车数据.快递单] Paddlenlp之UIE分类模型[以情感倾向分析新闻分类为例]含智能标注方案) 应用实践:分类模型大集成者[Pad ...