[原创](Modern C++)现代C++的字符串与Windows API交互的正确方式.

[简介]

常用网名: 猪头三

出生日期: 1981.XX.XX

QQ联系: 643439947

个人网站: 80x86汇编小站 https://www.x86asm.org

编程生涯: 2001年~至今[共22年]

职业生涯: 20年

开发语言: C/C++、80x86ASM、PHP、Perl、Objective-C、Object Pascal、C#、Python

开发工具: Visual Studio、Delphi、XCode、Eclipse、C++ Builder

技能种类: 逆向 驱动 磁盘 文件

研发领域: Windows应用软件安全/Windows系统内核安全/Windows系统磁盘数据安全/macOS应用软件安全

项目经历: 磁盘性能优化/文件系统数据恢复/文件信息采集/敏感文件监测跟踪/网络安全检测

[序言]

在Windows系统中, 提供了大量的纯C接口的API, 比如最经典的SetWindowsText(), GetWindowsText(), 非常具有代表性, 因为它们涉及到了C语言的字符串指针的传递. 现在随着(Modern C++)现代C++的普及性, 为了代码更加纯粹和简单, 现在大多数都是用STL String的方式来处理指针的变换, 从而避免使用高损耗的CString类或者各种奇怪的ATL类.

[第一种方式: std::wstring 处理]

cpp 复制代码
​​​​​​std::wstring wstr_Test ;
::SetWindowTextW(hWnd, wstr_Test.c_str()) ;

[第二种方式: std::vector 分配字符串内存]

cpp 复制代码
// +1 表示预留一个NUL结尾
const int int_Length = ::GetWindowsTextLength(hWnd) + 1 ;
std::vector<wchar_t> verctor_Buffer(int_Length) ;

[第三种方式: std::unique_ptr 配合 new 分配字符串内存]

cpp 复制代码
// +1 表示预留一个NUL结尾
const int int_Length = ::GetWindowsTextLength(hWnd) + 1 ;
std::unique_ptr<wchar_t[]> whar_Buffer(new wchar_t[int_Length]) ;

//或者

// +1 表示预留一个NUL结尾
const int int_Length = ::GetWindowsTextLength(hWnd) + 1 ;
auto whar_Buffer = std::make_unique<wchar_t[]>(int_Length)

[std::vector, std::unique_ptr<wchar_t[]>如何接收字符串数据内容?]

1> std::vector<wchar_t> 方式接收, verctor_Buffer.data() 返回std::vector<wchar_t>管理的原始缓冲区起始指针

cpp 复制代码
​​​​​​​::GetWindowTextW(hWnd, verctor_Buffer.data(), int_Length) ;

2> std::unique_ptr<wchar_t[]> 方式接收, whar_Buffer.get() 返回std::unique_ptr<wchar_t[]>管理的原始缓冲区起始指针

cpp 复制代码
::GetWindowTextW(hWnd, whar_Buffer.get(), int_Length) ;

3> 接受到字符串数据之后, 可以使用深度复制, 赋值给其他变量

cpp 复制代码
​​​​​​​std::wstring wstr_Text(verctor_Buffer.data()) ;
// 或者
std::wstring wstr_Text(whar_Buffer.get()) ;

[std::wstring如何接受字符串数据内容?][值得注意与学习]

cpp 复制代码
const int int_Length = ::GetWindowsTextLength(hWnd) + 1 ;
std::wstring wstr_Text ;
wstr_Test.resize(int_Length) ;
::GetWindowTextW(hWnd, &wstr_Text[0], int_Length) ;
wstr_Test.resize(int_Length-1) ;

特别注意:
1> &wstr_Text[0], 表示获取std::wstring的原始缓冲区起始指针, 该缓冲区是具有可写权限
2> wstr_Test.resize(int_Length-1) ; 避免双NUL终止符号. 因为std::wstring的resize()会自动添加一个NUL终止符号, 由于之前::GetWindowsTextLength(hWnd) + 1代码已保证创建了一个NUL终止符号, 所以导致了双NUL终止符号, 因此需要通过wstr_Test.resize(int_Length-1)来去掉一个NUL终止符号.

[另外一个细节: 通过std::unique_ptr类型, 可以方便的反复创建合适大小的内存, 而不用担心内存泄漏]

由于有些Windows API, 在传递参收之前需要预分配内存空间, 但是由于大小未知, 因此需要通过循环的方式去确认所需的内存大小, 比如经典的错误标志位: ERROR_MORE_DATA, 有了(Modern C++)现代C++的std::unique_ptr类型, 处理更加方便.

cpp 复制代码
LONG long_error = ERROR_MORE_DATA;
auto whar_Buffer ;
DWORD dword_BufferLength =  /*根据当前实际情况初始化一个合理的大小*/
while (long_error == ERROR_MORE_DATA)
{
    // 分配内存
    whar_Buffer = std::make_unique<wchar_t[]>(dword_BufferLength);
    // 调用某个Windows API, 查询大小
    error = ::SomeApiCall(param1, param2, ..., whar_Buffer.get(), &dword_BufferLength);
}

// 成功获取字符串内容, 赋值到其他变量
std::wstring wstr_Text(whar_Buffer.get());

[总结]

上面的代码使用率是非常高的, 希望多大家有所帮助.

相关推荐
程序员徐师兄2 小时前
Windows JDK11 下载安装教程,适合新手
java·windows·jdk11 下载安装·jdk11 下载教程
Bella的成长园地4 小时前
面试中关于 c++ async 的高频面试问题有哪些?
c++·面试
彷徨而立4 小时前
【C/C++】什么是 运行时库?运行时库 /MT 和 /MD 的区别?
c语言·c++
qq_417129254 小时前
C++中的桥接模式变体
开发语言·c++·算法
编码者卢布5 小时前
【App Service】Java应用上传文件功能部署在App Service Windows上报错 413 Payload Too Large
java·开发语言·windows
No0d1es6 小时前
电子学会青少年软件编程(C语言)等级考试试卷(三级)2025年12月
c语言·c++·青少年编程·电子学会·三级
bjxiaxueliang7 小时前
一文掌握C/C++命名规范:风格、规则与实践详解
c语言·开发语言·c++
多来哈米7 小时前
openclaw在Windows部署
windows·openclaw
视觉AI7 小时前
【踩坑实录】Windows ICS 共享网络下,国产化盒子 SSH 连接异常的完整分析
网络·windows·ssh
xu_yule8 小时前
网络和Linux网络-13(高级IO+多路转接)五种IO模型+select编程
linux·网络·c++·select·i/o