简体   繁体   English

设置高位设置是否正常?

[英]Is it normal for an hwnd to have its high bit set?

I'm passing my HWND to a sub-process so it can send me messages back on its progress. 我将HWND传递给子流程,以便它可以向我发送有关进度的消息。 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. 在调查过程中,我发现我传递给子流程的GetSafeHwnd()似乎返回的值不是我所期望的。

For example: 0xffffffffa5400382 例如:0xffffffffa5400382

Based on that, I can probably deduce that I'm not properly converting that value to/from an int64/string properly. 基于此,我可能可以推断出我没有正确地将值转换为int64 / string。 I can fix that. 我可以解决。 But what I find odd is that this hwnd just doesn't look right? 但是我发现奇怪的是,这个hwnd看起来不正确吗?

Are there scenarios where an HWND can have it's high bit set? HWND是否可以设置高位? Is this a normal window, or is there something special about the hwnd to end up like that? 这是正常的窗口,还是hwnd的某些特殊之处最终会导致这种情况?

I'm in C++, this is an CDialog based application window. 我在C ++中,这是一个基于CDialog的应用程序窗口。

The result you are seeing comes from sign extension of the handle value to a 64-bit integer. 您看到的结果是将句柄值的符号扩展为64位整数。 The actual handle value is 0xa5400382 , because handle values are always in the 32-bit range , even if the process is 64-bit! 实际的句柄值为0xa5400382 ,因为即使进程为64位, 句柄值也始终在32位范围内

So you should cast the HWND to std::uint32_t instead and convert that to string (or the other way around). 因此,您应该将HWND std::uint32_t转换为std::uint32_t并将其转换为字符串(或者反过来)。

Convert HWND to wstring: 将HWND转换为wstring:

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

Convert wstring to HWND: 将wstring转换为HWND:

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

The try/catch block is required because std::stoul() may throw exceptions if the conversion fails. 需要try / catch块,因为如果转换失败, std::stoul()可能会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM