惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
N
News and Events Feed by Topic
S
Schneier on Security
G
GRAHAM CLULEY
Scott Helme
Scott Helme
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
Project Zero
Project Zero
MyScale Blog
MyScale Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
The Last Watchdog
The Last Watchdog
Vercel News
Vercel News
The Cloudflare Blog
C
Check Point Blog
Help Net Security
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
S
Securelist
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
S
Secure Thoughts

博客园 - 新西兰程序员

C++ 构造函数中的初始化参数列表initializer list C++中的线程thread,以及C++11中的std::atomic学习 Leetcode56 Merge Intervals 合并区间 -- C++实现 C++中的内存对齐 C++中的static关键字使用以及全局变量 C++17中的结构化绑定Structured Binding C++中的函数参数是指针类型, 是按值传递 C++中异常处理机制中的栈展开stack unwinding C++实现链表反转 Linux中的性能分析工具 perf 来分析C++性能 C++17中新建一个类时,编译器默认生成的类成员函数 QT中的元对象系统 Leetcode 114 - 二叉树展开为链表 Leetcode65 有效数字 - 判断给定的字符串是否是一个有效数字 LettCode2289-Steps to Make Array Non-descreasing C#中的委托详解 C++中的std::function C++中的仿函数Functor C++中的const和constexpr异同比较 C++中的左值和右值,以及右值引用,移动语义 C++中传递参数是指针类型以及传入参数是指针的指针(**)详解 C++中GetTickCount函数学习 C++中以类的成员函数作为Windows callback函数需要设置成static函数 C#中的System.Security.SecureString学习 C++中的悬挂指针和野指针 C++中四种不同的对象生存方式(in stack, in heap, global, local static) C#中使用Parallel类来进行多线程并发编程 C++中的虚函数和虚函数表 C++中基类指针指向派生类对象 数据结构 - 栈的学习
String类型转LPCTSTR -----理解C++中的字符串类型转换
新西兰程序员 · 2024-04-12 · via 博客园 - 新西兰程序员

在看代码时,发现有时候会把string类型转换为LPCTSTR, 刚开始不理解为什么要做这个转换,所以做了一些调查,现在记录如下

是这样的,STRING是代表C++中的字符串string, 而LPCTSTR代表的是Windows系统中的字符串类型。 也就是说,这样转换的目的是为了把C++中的字符串string转换为Windows系统中的字符串类型LPCTSTR

我们知道,LPCTSTR是一种指针类型,它指向的是一个 const TCHAR数组,其中TCHAR数组中的字符可以是字符(char), 也可以是宽字符(wchar_t). => 所以 string 转LPCTSTR的目的就是为了使C++程序可以和Windows API进行交互, 因为Windows API只接受LPCTSTR作为字符串参数

 这是最常见的情况,就是和Windows API交互时,因为Windows API只接受LPCTSTR作为字符串参数, 所以在调用Windows API之前,必须先将C++字符串转换为LPCTSTR

另外一种情况就是 在C++中需要使用宽字符串,来支持多语言环境(有的语言需要宽字符串来存储),这种时候也可以把string转换为LPCTSTR(支持宽字符串)

方法:

使用string转LPCTSTR的方法有2个,一个是c_str()函数,另一个是使用_tcscpy()函数

LPCTSTR str = testString.c_str();
TCHAR buffer[256];

  _tcscpy(buffer, testString.c_str());

  LPCTSTR str = buffer;