c++windowswinapikernelhandles

Which Win32 user-mode handles can be shared among processes?


I've been trying to better understand WinAPIs and the underlying Windows kernel. From what I see so far, HWND (window) handles can be shared among processes.

But what about other handles, can they be shared among processes?

HMODULE, HINSTANCE, ATOM?

As well as window and drawing components:

HMENU, HDC, HICON, HCURSOR, HBRUSH, etc.?

And lastly, if all of the above cannot be shared, are there any user-mode handles that can be?

EDIT: Clarification. By sharing, I meant taking the handle's numerical value from one process and using it in another process.


Solution

  • HMODULE and HINSTANCE is the same thing on 32 and 64-bit Windows. It is the base address of a module and cannot really be shared across processes (except the addresses of ntdll and kernel32 if the other process has the same bitness).

    ATOMs come in per-process and global forms and the global form can be shared by processes on the same windowstation.

    The GDI drawing handles cannot be shared (except for maybe GetStockObject?).

    Under Windows NT, GDI objects are stored in the client part of the Win32 subsystem's GDI module. That means that any GDI object is valid only in the context of the application that created it.

    USER32 handles (HWND, HMENU, HICON, HCURSOR) can be shared.

    DuplicateHandle accepts desktop and windowstation handles...