Difference between HANDLE and HWND in Windows API?
Asked Answered
D

3

28

I'm trying to use function SetForegroundWindow(HWND hWnD). I have some handles but it's not working as parameter of above function. My handle is a thread and I want to run it in foreground.

What are the differences between a HWND and a HANDLE?

Dingdong answered 4/8, 2013 at 6:48 Comment(0)
F
44

They are just abstract data types.

According to MSDN, HANDLE and HWND are defined as:

  • HANDLE is a handle to an object.
  • HWND is a handle to a window.

So, a HWND is a HANDLE, but not all HANDLEs are HWND. In fact:

typedef void *PVOID;
typedef PVOID HANDLE;
typedef HANDLE HWND;

Example

You should only pass HWND to SetForegroundWindow unless you know what you are doing.

HWND hWnd = FindWindow(NULL, "Calculator");
SetForegroundWindow(hWnd);

This first gets the handle to a window titled "Calculator" with FindWindow and then brings that window to foreground.

Foozle answered 4/8, 2013 at 6:57 Comment(3)
Those typedefs are only used if STRICT is not defined. If it is, an HWND is not defined as a HANDLE anymore, but as a unique and independent data type.Aulos
@RemyLebeau The typedef part was quoted from the offical document. (But I checked the header, that is defined the way you described. Thanks.)Foozle
HWND is certainly unrelated to HANDLE even if they happen to compile using the same pointer-sized type. HWND is an index into a data structure in the windowing component (user32.dll and friends), HANDLE is an index into data structures in the kernel.Lasonde
N
15

A "handle" is the general term used to refer to a token that identifies a resource on the system (a menu, a DLL module, a block of memory, etc). Often referred to as a "magic cookie", it's normally returned when you first create the resource. You then pass that handle to other functions in the API responsible for processing the resource. You normally need not know what the handle is however. Sometimes it may be a pointer, other times a number, perhaps a structure, or whatever. That's why they hide it using names like HWND which is simply the handle used to identify a window (returned by the API function "CreateWindow()"). You therefore don't convert a "handle" to an HWND and back again since an HWND is already a "handle" (one that merely identifies windows you create).

Found here http://forums.codeguru.com/showthread.php?135438-Handle-and-HWND

You can use FindWindow to get the hwnd from an application http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx

This should allow you to get the HWND provided you have the handle to what you're looking for C++ Handle as HWND?

Negative answered 4/8, 2013 at 6:51 Comment(5)
yep, tks!, but can i convert from handle to hwnd ?Dingdong
@Dingdong This is like asking "Can I convert from void * to int * ?" It depends. What's your handle?Foozle
My handle is a thread and I want to run it in foreground, but it's not a HWND in function SetForegroundWindow(HWND hWnD).Dingdong
I cast it HWND hWnD = (HWND) myHanle; That not error but I can't check it run or not.Dingdong
@Dingdong SetForegroundWindow is an API to manipulate window, not thread.Foozle
S
11

The HWND is also a HANDLE, but a global one. I.e. a HWND valid in the context of one process is also valid in the context of another process.

Some undocumented info at https://winterdom.com/dev/ui/wnd/.

Sublunary answered 7/11, 2015 at 11:18 Comment(4)
"but a global one" is exactly what I was looking for. Thx mate.Sidestroke
That link is now dead.Pressurize
An HWND is a sort of handle, but not a HANDLE.Lasonde
@BenVoigt both types are typedefs to pointers, therefore at the C++or OS level they are equal.Sublunary

© 2022 - 2024 — McMap. All rights reserved.