Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are winapi handles global?

Tags:

winapi

A very simple question, if i create a HANDLE in app1.exe and it gets value 0x01 is that value globally unique ?

Or is it possible that when some other process creates a HANDLE that also has value 0x01.

If they are not unique what other construct can i use to get a unique id compatible with handles (such that it will be impossible or highly unlikely that a HANDLE with that id is created anywhere else).

like image 607
n00b Avatar asked Mar 22 '12 20:03

n00b


People also ask

What is a handle Winapi?

Properly, in Windows, (and generally in computing) a handle is an abstraction which hides a real memory address from the API user, allowing the system to reorganize physical memory transparently to the program. Resolving a handle into a pointer locks the memory, and releasing the handle invalidates the pointer.

What is handle in MFC?

The handle wrapping functions of the MFC class library let you find the C++ object that is wrapping the Windows object that has a particular handle. However, sometimes an object does not have a C++ wrapper object and at these times the system creates a temporary object to act as the C++ wrapper.

How many handles can Windows handle?

In one of the rare cases where Windows sets a hard-coded upper limit on a resource, the Executive defines 16,777,216 (16*1024*1024) as the maximum number of handles a process can allocate.

What is an application handle?

In computer programming, a handle is an abstract reference to a resource that is used when application software references blocks of memory or objects that are managed by another system like a database or an operating system.


2 Answers

The important thing to understand is that handles are not objects. Handles are pointers (or indexes) to per-process object table. To answer your question, HANDLES are not globally unique, but they are scoped to only make sense inside a particular process.

For any kernel object to be able to be accessible from other process, you have to DuplicateHandle.

Another way to share objects across processes is to call CreateProcess with bInheritHandles set to true.

like image 61
seva titov Avatar answered Nov 05 '22 13:11

seva titov


They are not unique. HANDLE values are local to the current process. Same value may be invalid handle or refer to a different object in another process. An exception to this rule are handles inherited from parent process.

The only way to have unique id without a centralized registry is to use GUID. But they are not compatible with HANDLE, they are 128-bit while handles are 32 or 64-bit.

like image 39
hamstergene Avatar answered Nov 05 '22 15:11

hamstergene