在C ++中将HWND转换为十六进制字符串 [英] Convert HWND to Hex String in C++

查看:227
本文介绍了在C ++中将HWND转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中将 HWND 转换为十六进制字符串的最佳方法是什么,我的意思是还使用"0x" 前缀吗?

What is the best way to convert HWND to Hex String in C++, I mean also with a "0x" prefix?

HWND hWnd = FindWindow(L"Notepad", L"Untitled - Notepad");
MessageBox(nullptr, LPCWSTR(hWnd), L"Hello World!", MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);

但是我希望它输出 0x00000000 (假设记事本窗口未打开),但是它总是返回一个空字符串.

But I expect this to output 0x00000000 (assuming Notepad windows is not open) , but it always returns an empty string.

我也尝试了这个答案,但最后我还是返回了<代码> 0000000000000000 .

I also tried this answer, but I ended up with returning 0000000000000000.

任何人都可以帮助我进行转化吗?

Anyone can help me on that conversion?

推荐答案

您要做的不是转换.您只需将 hWnd 强制转换为指向字符串的指针.几乎总是不会指向有效的字符串,当您尝试将其打印为字符串时会产生未定义的行为.

What you are doing is not conversion. You just cast hWnd to a pointer to string. Almost always it will not point to a valid string, producing an undefined behavior when you try to print it as a string.

要正确执行此操作,应该在消息框中显示hWnd的位的特征为整数并将其以十六进制形式打印到某个缓冲区:

To do it properly, you should trait hWnd's bit's as integer and print it to some buffer as hex before showing in the message box:

#include <sstream>
#include <cstdint>
#include <iomanip>

//.....

std::wstringstream ss;
ss << std::hex << L"0x" << std::setw(16) << std::setfill(L'0') << 
    *reinterpret_cast<uint64_t*>(&hWnd) << std::endl;
MessageBox(nullptr, ss.str().c_str(), L"Hello World!",
    MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);

注意:

1) stringstream 是C ++风格的 sprintf .它是 str()方法返回的 std :: string ,因此,要获取C风格的指针,应在其上调用 c_str .

1) stringstream is a C++-style sprintf. It's str() method returns std::string, so to get a C-style pointer you should call c_str on it.

2)我没有Windows来检查什么是HWND.因此,请检查其大小并使用适当的整数类型代替 uint64_t .重要的是,就好像您使用的类型太宽一样,也将导致垃圾甚至访问冲突.一种更好的方法是使用整数类型的模板,例如已讨论过的这里.

2) I have no Windows to check what is HWND actually. So please check it's size and use appropriate integer type instead of uint64_t. It's important, as if you use a too wide type, you'll get garbage or even access violation. A better approach is to use an integer type template like one discussed here.

3)在使用MessageBox的宽字符版本时,可能需要 std :: wstringstream .

3) Probably, you need std::wstringstream as you are using wide-char version of MessageBox.

4)装饰. ss<<* reinterpret_cast< uint64_t *>(& hWnd)只是将原始十六进制数字打印到 ss ,因此,要获得正确的格式,您应该对其进行微调,设置适当的填充和填充特点.例如,这将导致所有整数被打印为带有前导零的16位数字:

4) Decoration. ss << *reinterpret_cast<uint64_t*>(&hWnd) just prints raw hex digits to ss, so to get the right format, you should fine-tune it, setting proper padding and filling character. For example, this will cause all integers to be printed as 16-digit numbers with leading zeros:

ss << std::setw(16) << std::setfill(L'0') ...

其中 setw setfill 函数来自 iomanip 标头.另外,打印 0x 前缀是您的工作,而不是 stringstream 的工作.还可以看看 std :: showbase .

where setw and setfill functions are from iomanip header. Also, printing 0x prefix is your job, not stringstream's. Also take a look at std::showbase.

这篇关于在C ++中将HWND转换为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆