长到 HWND (VS8 C++)

发布于 2024-07-05 07:22:07 字数 239 浏览 6 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾冷 2024-07-12 07:22:08

HWND 是窗口句柄。
该类型在 WinDef.h 中声明如下:

typedef HANDLE HWND;

HANDLE 是对象的句柄。
该类型在 WinNT.h 中声明如下:

typedef PVOID 句柄;

最后,PVOID 是一个指向任意类型的指针。
该类型在 WinNT.h 中声明如下:

typedef void *PVOID;

所以,HWND实际上是一个指向void的指针。 您可以像这样将 long 转换为 HWND:

HWND h = (HWND)my_long_var;

但要非常小心 my_long_var 中存储的信息。 你必须确保那里有一个指针。

稍后编辑:
该警告表明您已打开 64 位可移植性检查。 如果您正在构建 32 位应用程序,则可以忽略它们。

HWND is a handle to a window.
This type is declared in WinDef.h as follows:

typedef HANDLE HWND;

HANDLE is handle to an object.
This type is declared in WinNT.h as follows:

typedef PVOID HANDLE;

Finally, PVOID is a pointer to any type.
This type is declared in WinNT.h as follows:

typedef void *PVOID;

So, HWND is actually a pointer to void. You can cast a long to a HWND like this:

HWND h = (HWND)my_long_var;

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.

红墙和绿瓦 2024-07-12 07:22:08

仅当您运行的不是 64 位版本的 Windows 时,这样做才是安全的。 LONG类型是32位,但是HANDLE类型可能是64位。 您需要使您的代码 64 位干净。 简而言之,您需要将 LONG 更改为 LONG_PTR。

使用指针类型的规则

不要将指针强制转换为 int、long、
ULONG,或双字。 如果您必须施放
测试某些位的指针,设置或
清除位,或以其他方式操纵
其内容,使用 UINT_PTR 或
INT_PTR 类型。 这些类型是不可或缺的
缩放到 a 大小的类型
32 位和 64 位的指针
Windows(例如,32 位的 ULONG
Windows 和 64 位的 _int64
视窗)。 例如,假设您是
移植以下代码:

图像库 = (PVOID)((ULONG)图像库 |
1);

作为移植过程的一部分,您
将代码更改如下:

图像库 =
(PVOID)((ULONG_PTR)ImageBase | 1);

使用 UINT_PTR 和 INT_PTR,其中
合适(如果您不确定
是否需要,没有
以防万一使用它们会造成伤害)。 做
不将指针强制转换为类型
ULONG、LONG、INT、UINT 或 DWORD。

请注意,HANDLE 被定义为
void*,因此对 HANDLE 值进行类型转换
到要测试、设置或的 ULONG 值
清除低位 2 位是错误
在 64 位 Windows 上。

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:

Do not cast pointers to int, long,
ULONG, or DWORD. If you must cast a
pointer to test some bits, set or
clear bits, or otherwise manipulate
its contents, use the UINT_PTR or
INT_PTR type. These types are integral
types that scale to the size of a
pointer for both 32- and 64-bit
Windows (for example, ULONG for 32-bit
Windows and _int64 for 64-bit
Windows). For example, assume you are
porting the following code:

ImageBase = (PVOID)((ULONG)ImageBase |
1);

As a part of the porting process, you
would change the code as follows:

ImageBase =
(PVOID)((ULONG_PTR)ImageBase | 1);

Use UINT_PTR and INT_PTR where
appropriate (and if you are uncertain
whether they are required, there is no
harm in using them just in case). Do
not cast your pointers to the types
ULONG, LONG, INT, UINT, or DWORD.

Note that HANDLE is defined as a
void*, so typecasting a HANDLE value
to a ULONG value to test, set, or
clear the low-order 2 bits is an error
on 64-bit Windows.

素罗衫 2024-07-12 07:22:08

只要您确定您拥有的 LONG 确实是 HWND,那么它就很简单:

HWND hWnd = (HWND)(LONG_PTR)lParam;

As long as you're sure that the LONG you have is really an HWND, then it's as simple as:

HWND hWnd = (HWND)(LONG_PTR)lParam;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文