Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HANDLE and HWND in Windows API?

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?

like image 364
Thangnv Avatar asked Sep 05 '25 15:09

Thangnv


1 Answers

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.

like image 96
timothyqiu Avatar answered Sep 09 '25 05:09

timothyqiu