Process Kernel Object Handles and Identifiers

< BACK  NEXT >
[oR]

The kernel object handles for the thread and process refer to data managed by the operating system relating to the thread or process. The operating system manages a reference count on the data whenever a handle is returned to an application (as is the case with CreateProcess) or the handle is copied, the reference count is incremented. When an application is finished with the handle, it must call CloseHandle for that handle. The reference count is decremented when the handle is closed.

The lifetime of the kernel object is not necessarily the same as the lifetime of the process that it represents. If the reference count is greater than 0 when the process terminates, the kernel object will not be deleted. This means that information about the process can still be obtained even after the process has terminated. It is important that an application does call CloseHandle on the kernel object handle when the application is finished with the handle, to ensure that the operating system can free resources associated with the kernel object.

Process and thread kernel object handles are process-relative, that is, they can only be used reliably in the process that obtained them. Unlike Windows NT/98/2000, the function DuplicateHandle is not implemented in Windows CE, and so handles cannot be duplicated to allow them to be passed to other processes.

Some functions require a process or thread identifier rather than a kernel object handle. These identifiers are DWORD values that are unique for the process or thread across the entire operating system and can therefore be safely passed from process to process. The function OpenProcess may be used to obtain a process handle from a process identifier:

 HANDLE hProcess; hProcess = OpenProcess(0, FALSE, dwProcessId); 

In Windows CE the first two parameters to OpenProcess are not supported and should be passed as 0 and "FALSE". As usual, the handle returned from OpenProcess should be closed by passing it to CloseHandle.

A process can determine its own process identifier by calling the function GetCurrentProcessId the function takes no arguments and returns a DWORD.

 DWORD dwProcessId; dwProcessId = GetCurrentProcessId(); 

The function GetCurrentProcess can be called to return a kernel object handle for the current process. You need to be careful when using this handle, as it is actually a "pseudohandle". The returned handle always refers to the current process, so if you pass the handle to another process, the handle refers to that second process. Note that you do not need to call CloseHandle on pseudohandles.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net