[原创](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\>如何接收字符串数据内容?]

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_Text0, 表示获取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());

总结

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

相关推荐
博客180016 小时前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴18 小时前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨1 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4566 天前
C++进阶(1)——前景提要
c++
夜悊6 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴6 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0016 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
qq_369224336 天前
Windows全系通用!ntdll.dll文件丢失、报错、闪退问题的完整排查与修复教程
windows·dll·dll修复·dll丢失·dll错误
玖玥拾6 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you7 天前
constexpr函数
c++