GetProcessHandleFromHwnd function

Retrieves a process handle from a window handle.


HANDLE WINAPI GetProcessHandleFromHwnd(
  _In_ HWND hwnd


hwnd [in]

Type: HWND

The window handle.

Return value


If successful, returns the handle of the process that owns the window.

If not successful, returns NULL.


In previous versions of the operating system, a process could open another process (to access its memory, for example) using OpenProcess. This function succeeds if the caller has appropriate privileges; usually the caller and target process must be the same user.

On Windows Vista, however, OpenProcess fails in the scenario where the caller has UIAccess, and the target process is elevated. In this case, the owner of the target process is in the Administrators group, but the calling process is running with the restricted token, so does not have membership in this group, and is denied access to the elevated process. If the caller has UIAccess, however, they can use a windows hook to inject code into the target process, and from within the target process, send a handle back to the caller.

GetProcessHandleFromHwnd is a convenience function that uses this technique to obtain the handle of the process that owns the specified HWND. Note that it only succeeds in cases where the caller and target process are running as the same user. The returned handle has the following privileges: PROCESS_DUP_HANDLE | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | SYNCHRONIZE. If other privileges are required, it may be necessary to implement the hooking technique explicitly instead of using this function.


Requirement Value
Minimum supported client
Windows XP [desktop apps only]
Minimum supported server
Windows Server 2003 [desktop apps only]