Difference between HANDLE and HWND in Windows API?

Thangnv picture Thangnv · Aug 4, 2013 · Viewed 54.1k times · Source

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?

Answer

timothyqiu picture timothyqiu · Aug 4, 2013

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.