长到 HWND (VS8 C++)
如何将 long 转换为 HWND (C++ Visual Studio 8)?
Long lWindowHandler;
HWND oHwnd = (HWND)lWindowHandler;
但我收到以下警告:
警告 C4312:“类型转换”:从“LONG”转换为更大尺寸的“HWND”
谢谢。
How can I cast long to HWND (C++ visual studio 8)?
Long lWindowHandler;
HWND oHwnd = (HWND)lWindowHandler;
But I got the following warning:
warning C4312: 'type cast' : conversion from 'LONG' to 'HWND' of greater size
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HWND 是窗口句柄。
该类型在 WinDef.h 中声明如下:
HANDLE 是对象的句柄。
该类型在 WinNT.h 中声明如下:
最后,PVOID 是一个指向任意类型的指针。
该类型在 WinNT.h 中声明如下:
所以,HWND实际上是一个指向void的指针。 您可以像这样将 long 转换为 HWND:
但要非常小心 my_long_var 中存储的信息。 你必须确保那里有一个指针。
稍后编辑:
该警告表明您已打开 64 位可移植性检查。 如果您正在构建 32 位应用程序,则可以忽略它们。
HWND is a handle to a window.
This type is declared in WinDef.h as follows:
HANDLE is handle to an object.
This type is declared in WinNT.h as follows:
Finally, PVOID is a pointer to any type.
This type is declared in WinNT.h as follows:
So, HWND is actually a pointer to void. You can cast a long to a HWND like this:
but very careful of what information is stored in my_long_var. You have to make sure that you have a pointer in there.
Later edit:
The warning suggest that you've got 64-bit portability checks turned on. If you're building a 32 bit application you can ignore them.
仅当您运行的不是 64 位版本的 Windows 时,这样做才是安全的。 LONG类型是32位,但是HANDLE类型可能是64位。 您需要使您的代码 64 位干净。 简而言之,您需要将 LONG 更改为 LONG_PTR。
使用指针类型的规则:
Doing that is only safe if you are not running on a 64 bit version of windows. The LONG type is 32 bits, but the HANDLE type is probably 64 bits. You'll need to make your code 64 bit clean. In short, you will want to change the LONG to a LONG_PTR.
Rules for using pointer types:
只要您确定您拥有的 LONG 确实是 HWND,那么它就很简单:
As long as you're sure that the LONG you have is really an HWND, then it's as simple as: