Difference between HANDLE and HWND in Windows API?

落花浮王杯 提交于 2020-01-20 17:05:24

问题


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?


回答1:


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.




回答2:


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?




回答3:


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 http://forum.sysinternals.com/internal-wnd-structure-in-win7_topic24988.html.



来源:https://stackoverflow.com/questions/18040440/difference-between-handle-and-hwnd-in-windows-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!