0

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.

For example: 0xffffffffa5400382

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?

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?

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

4

1 回答 1

3

您看到的结果来自将句柄值符号扩展为 64 位整数。实际的句柄值是0xa5400382,因为句柄值总是在 32 位范围内,即使进程是 64 位的!

因此,您应该HWND改为std::uint32_t将其转换为字符串(或相反)。

将 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()如果转换失败可能会抛出异常。

于 2017-03-25T14:09:48.930 回答