这种错误,很多情况下是类型不匹配

LPTSTR表示为指向常量TCHAR字符串的长指针

TCHAR可以是wchar_tchar,基于项目是多字节还是宽字节版本。

看下面的代码,代码来源: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'错误的更多相关文章

  1. 【转】char*,const char*和string的相互转换

    1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...

  2. string,const char*,char*之间的相互转换

    1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...

  3. C++ char*,const char*,string的相互转换

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

  4. [转载] C++ string, const char*, char* 之间互相转换

    1, string转const char* 类型 string str = "abcdef"; const char* con_str = string.c_str() 2, co ...

  5. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  6. char*,const char*和string的相互转换

    好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1 ...

  7. CString、string、const char*的相互转换

    环境:vs2010 1.CString转string //第一种方式: CString str = _T("CSDN"); USES_CONVERSION; std::string ...

  8. (c++) int 转 string,char*,const char*和string的相互转换

    一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...

  9. 【转载】char*,const char*和string 三者转换

    本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678 const char* 和string 转换 const char*转换 ...

  10. 转载:string、const char*、 char* 、char[]相互转换

    本文转自:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 一:转化总结形式如下: 使用时,要对源格式和目标格式进行初始化 ...

随机推荐

  1. ext4 磁盘扩容

    目录 ext4文件系统磁盘扩容 目标 途径 操作步骤 改变前的现状 操作和改变后的状态 ext4文件系统磁盘扩容 一个磁盘有多个分区,分别创建了物理卷.卷组.逻辑卷.通过虚拟机软件对虚拟机的磁盘/de ...

  2. [转帖]​Linux开源存储漫谈(2)IO性能测试利器fio

    fio(Flexible I/O Tester)正是非常常用的文件系统和磁盘 I/O 性能基准测试工具.提供了大量的可定制化选项,可以用来测试,裸盘.一个单独的分区或者文件系统在各种场景下的 I/O ...

  3. Crash的简单学习

    Crash的简单学习 前言 最近进行海光服务器的压测, 多次出现了压测时宕机的情况. 跟OS,DB还有hardware的vender都进行过沟通, 但都比较难定位具体问题. 麒麟操作系统说需要进行一下 ...

  4. [转帖]/etc/profile 和 /etc/profile.d/ 的区别

    https://my.oschina.net/calmsnow/blog/2989570   /etc/profile 是文件, /etc/profile.d/ 是目录,用在设置环境变量方面,/etc ...

  5. IPMI的简单使用

    背景 公司一台十一年前的服务器砸到我手中,要重装CentOS7的操作系统. 本着不想进机房, 不想格式化U盘的想法, 想用BMC进行安装系统. 遇到的第一个问题是不知道密码. 询问之前的机器持有人,也 ...

  6. 【计数,DP】ABC306Ex Balance Scale

    Problem Link 现在有 \(n\) 个球,每个球有一个重量,重量未知.接下来会进行 \(m\) 次称重,每次给定 \(a_i\) 和 \(b_i\),比较这两个球的重量,结果可能是 \(&g ...

  7. Milvus性能优化提速之道:揭秘优化技巧,避开十大误区,确保数据一致性无忧,轻松实现高性能

    Milvus性能优化提速之道:揭秘优化技巧,避开十大误区,确保数据一致性无忧,轻松实现高性能 Milvus 是全球最快的向量数据库,在最新发布的 Milvus 2.2 benchmark中,Milvu ...

  8. 2.1 PE结构:文件映射进内存

    PE结构是Windows系统下最常用的可执行文件格式,理解PE文件格式不仅可以理解操作系统的加载流程,还可以更好的理解操作系统对进程和内存相关的管理知识,在任何一款操作系统中,可执行程序在被装入内存之 ...

  9. List对象按属性排序

    1.Stream流sorted 正序: List<Person> collect = personList.stream().sorted(Comparator.comparing(Per ...

  10. C/C++中的可变参数和可变参数模板

    目录 1.说明 2.C语言中的可变参数 3.C++中的可变参数模板 2.1.使用递归的方式遍历 2.2.使用非递归的方式遍历 1.说明 不谈官方定义,就从个人理解上说,可变参数 就是函数传参的时候,不 ...