Long to HWND (VS8 C ++)

How can I use long for HWND (C ++ visual studio 8)?

Long lWindowHandler; HWND oHwnd = (HWND)lWindowHandler; 

But I received the following warning:

warning C4312: "cast type": conversion from "LONG" to "HWND" larger

Thanks.

+7
long-integer casting hwnd
source share
3 answers

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

typedef HANDLE HWND;

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

typedef PVOID HANDLE;

Finally, the 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 use long for HWND as follows:

HWND h = (HWND) my_long_var;

but very carefully what information is stored in my_long_var. You must make sure you have a pointer.

Next, edit: Warning tells you that you have enabled 64-bit portability checks. If you are building a 32-bit application, you can ignore them.

+8
source share

As long as you are sure that you have LONG HWND, then this is simple:

 HWND hWnd = (HWND)(LONG_PTR)lParam; 
+9
source share

This is 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 need to make your code 64-bit. In short, you will want to change LONG to LONG_PTR.

Rules for using pointer types :

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

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

As part of the migration process, you will modify the code as follows:

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

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

Note that HANDLE is defined as void *, so casting the HANDLE value to the ULONG value to test, install, or clear the lower 2 bits is an error on 64-bit Windows.

+5
source share

All Articles