设置高位是否正常? [英] Is it normal for an hwnd to have its high bit set?

查看:110
本文介绍了设置高位是否正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将HWND传递给子流程,以便它可以向我发送有关其进度的消息.有时,我从未从子流程中收到任何消息. 在调查过程中,我发现我传递给子流程的GetSafeHwnd()似乎返回的值不是我所期望的.

I'm passing my HWND to a sub-process so it can send me messages back on its progress. Occasionally I never receive any messages from the sub-process. While investigating, I've found that GetSafeHwnd() of which I'm passing to the subprocess seems to be returning values I don't expect.

例如: 0xffffffffa5400382

For example: 0xffffffffa5400382

基于此,我大概可以推断出我没有正确地将值转换为int64/string.我可以解决.但是我感到奇怪的是,这个hwnd看起来不正确吗?

Based on that, I can probably deduce that I'm not properly converting that value to/from an int64/string properly. I can fix that. But what I find odd is that this hwnd just doesn't look right?

在某些情况下,HWND可以设置高位吗?这是正常的窗口吗,还是hwnd的某些特殊之处最终导致那样的事情?

Are there scenarios where an HWND can have it's high bit set? Is this a normal window, or is there something special about the hwnd to end up like that?

我在C ++中,这是一个基于CDialog的应用程序窗口.

I'm in C++, this is an CDialog based application window.

推荐答案

您看到的结果来自句柄值的符号扩展为64位整数.实际的句柄值为0xa5400382,因为

The result you are seeing comes from sign extension of the handle value to a 64-bit integer. The actual handle value is 0xa5400382, because handle values are always in the 32-bit range, even if the process is 64-bit!

因此,您应该将HWND强制转换为std::uint32_t并将其转换为字符串(或相反).

So you should cast the HWND to std::uint32_t instead and convert that to string (or the other way around).

将HWND转换为wstring:

HWND hwnd = GetSafeHwnd();
std::uint32_t handleValue = reinterpret_cast<std::uint32_t>( hwnd );
std::wstring handleValueStr = std::to_wstring( handleValue );

将wstring转换为HWND:

try
{
    std::uint32_t handleValue = std::stoul( someString );
    HWND handle = reinterpret_cast<HWND>( handleValue );
}
catch( std::exception& e )
{
    // Handle string conversion error
}

try/catch块是必需的,因为 std::stoul() 如果转换失败,可能会引发异常.

The try/catch block is required because std::stoul() may throw exceptions if the conversion fails.

这篇关于设置高位是否正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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