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

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. [转帖]jmeter正则表达式提取器获取数组数据-02篇

    接上篇,当我们正则表达式匹配到多个值以后,入下图所示,匹配到21个结果,如果我们想一次拿到这一组数据怎么办呢 打开正则表达式提取器页面,匹配数字填入-1即可 通过调试取样器就可以看到匹配到已经匹配到多 ...

  2. [转帖]将 Cloudflare 连接到互联网的代理——Pingora 的构建方式

    https://zhuanlan.zhihu.com/p/575228941 简介 今天,我们很高兴有机会在此介绍 Pingora,这是我们使用 Rust 在内部构建的新 HTTP 代理,它每天处理超 ...

  3. [转帖]CentOS8完美升级gcc版本方法

    https://blog.whsir.com/post-6114.html 在CentOS8系统中,默认gcc版本已经是8.x.x版本,但是在一些场景中,还是需要高版本的gcc,网上一些作死的文章还在 ...

  4. Nginx 发布 Docker 运行日志的方法

    背景 公司这边想进行容器化负载均衡部署. 脚本很简单, 已经实现了, 但是发现我这边没有ELK也没有LOKI 又不太像切入到容器内部进行 获取日志信息. 所以我这边想了一个别的招来动态刷新日志. 思路 ...

  5. Linux KVM网络处理过程

    Linux KVM网络处理过程 总体解决方法 本次遇到的问题是KVM的网桥处理不小心导致系统无法连接.处理简要总结: 进入机房,给IPMI插上网线, 开机点 Del 进入bios 设置IMPI的地址 ...

  6. docker hub arm64v8/postgres

    arm64v8/postgres https://hub.docker.com/r/arm64v8/postgres By arm64v8 • Updated 4 days ago The Postg ...

  7. with(上下文管理器)的用法

    with语句可以自动管理上下文资源,不论什么原因(成功或失败)跳出with语句,都能保证文件正确关闭,并 释放资源,不用手动去close掉资源 1.with语句中有两个内置方法__enter__和__ ...

  8. C# Contains()、 == 和Equals() 比较

    目前在开发中前端页面有搜索条件 和后端的方法进行匹配 有一个Student表,其中有字段:name.age.address.tel CREATE TABLE Student ( Name varcha ...

  9. 数据挖掘机器学习[五]---汽车交易价格预测详细版本{模型融合(Stacking、Blending、Bagging和Boosting)}

    题目出自阿里天池赛题链接:零基础入门数据挖掘 - 二手车交易价格预测-天池大赛-阿里云天池 相关文章: 特征工程详解及实战项目[参考] 数据挖掘---汽车车交易价格预测[一](测评指标:EDA) 数据 ...

  10. Linux文件IO之二 [补档-2023-07-21]

    8-5 linux系统IO函数: open函数: ​ 函数原型:int open(const char *pathname, int flags, mode_t mode); ​ 功能:打开一个文件并 ...