5

As I understand from the MSDN documentation on Windows Data Types, a HWND is equivalent to a void*:

HWND - A handle to a window. This type is declared in WinDef.h as follows: typedef HANDLE HWND; HANDLE - A handle to an object. This type is declared in WinNT.h as follows: typedef PVOID HANDLE; PVOID - A pointer to any type. This type is declared in WinNT.h as follows: typedef void *PVOID;

However, if I try the following:

int foo;
HWND bar = &foo;

My compiler (VS2012) complains:

error C2440: '=' : cannot convert from 'int *' to 'HWND'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

I can't figure out the reason why. I've established that it's not related to the use of typedefs since the following compiles fine:

typedef void* MyType;
int foo;
MyType bar = &foo;

What is preventing me from assigning the address of an arbitrary object to a HWND?

The reason I want to do this, in case anyone objects to me trying to do it in the first place, is that I have some code involving HWNDs that I would like to unit test by supplying the HWNDs with known values that I can test for.

2
  • Why are you trying to assign something to an HWND variable that is obviously not a handle to a window?
    – selbie
    Jan 13, 2013 at 23:37
  • 1
    @selbie I already answered this in the final paragraph of my question.
    – JBentley
    Jan 13, 2013 at 23:46

3 Answers 3

8

If STRICT is defined during the compile, an HWND is defined as a pointer to a dummy struct instead of a void*.

One of the reasons that STRICT was added was to enable the compiler to catch the kind of implicit conversions that you want to do (and many people really didn't). Since you actually want the 'loose checking', make sure that STRICT isn't defined.

Or just cast.

(Note that the comments by yic81 on the MSDN documentation page you link to indicate that it's in need of some updating)

2
  • Thanks. I'm trying to understand more about STRICT - is this specifically a Windows thing? The references to it I've found on MSDN only talk about HANDLEs and such-like.
    – JBentley
    Jan 12, 2013 at 20:40
  • 2
    STRICT is just a macro introduced in the WinSDK a long time ago to configure some stricter type checking in the SDK headers. For example, instead of having all handles be the same void* type, they become pointers to separate types so the compiler will be able to check if you use one type of handle mistakenly where another type should be used. For example, without STRICT, an HWND is the same type as an HBITMAP so if you pass an HWND where an HBITMAP is expected, the compiler won't complain. STRICT fixes that. See support.microsoft.com/kb/83456 for some details. Jan 12, 2013 at 23:42
2

Try

int foo;
HWND bar = (HWND)&foo;
0

I found that long long is castable to HWND:

long long llHwnd = 133282;
HWND hwnd = (HWND)llHwnd;

I'll include the link to the original answer if I find one.

1
  • This will probably work only on 64 bit
    – Mario
    Feb 10, 2023 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.