8 out of 47 rated this helpful - Rate this topic

OpenProcess function

Applies to: desktop apps only

Opens an existing local process object.

Syntax

HANDLE WINAPI OpenProcess(
  __in  DWORD dwDesiredAccess,
  __in  BOOL bInheritHandle,
  __in  DWORD dwProcessId
);

Parameters

dwDesiredAccess [in]

The access to the process object. This access right is checked against the security descriptor for the process. This parameter can be one or more of the process access rights.

If the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.

bInheritHandle [in]

If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.

dwProcessId [in]

The identifier of the local process to be opened.

If the specified process is the System Process (0x00000000), the function fails and the last error code is ERROR_INVALID_PARAMETER. If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last error code is ERROR_ACCESS_DENIED because their access restrictions prevent user-level code from opening them.

Return value

If the function succeeds, the return value is an open handle to the specified process.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

To open a handle to another local process and obtain full access rights, you must enable the SeDebugPrivilege privilege. For more information, see Changing Privileges in a Token.

The handle returned by the OpenProcess function can be used in any function that requires a handle to a process, such as the wait functions, provided the appropriate access rights were requested.

When you are finished with the handle, be sure to close it using the CloseHandle function.

Examples

For an example, see Taking a Snapshot and Viewing Processes.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

AssignProcessToJobObject
CloseHandle
CreateProcess
CreateRemoteThread
DuplicateHandle
GetCurrentProcess
GetCurrentProcessId
GetExitCodeProcess
GetModuleFileNameEx
GetPriorityClass
Process and Thread Functions
Processes
ReadProcessMemory
SetPriorityClass
SetProcessWorkingSetSize
TerminateProcess
VirtualProtectEx
WriteProcessMemory

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
F#
[<DllImport("kernel32.dll")>] extern nativeint OpenProcess([<In>] uint32 dwDesiredAccess, [<In>] bool bInheritHandle, [<In>] uint32 dwProcessId)
  • 6/28/2011
  • a_a
F#
[<DllImport("kernel32.dll")>] extern nativeint OpenProcess([<In>] uint32 dwDesiredAccess, [<In>] bool bInheritHandle, [<In>] uint32 dwProcessId)
  • 6/28/2011
  • a_a
C#
[DllImportAttribute("kernel32.dll", EntryPoint = "OpenProcess")]
       public static extern IntPtr OpenProcess(int dwDesiredAccess,bool bInheritHandle,int dwProcessId );
Returning handle when process dead is right behavior
The behavior you notice of getting a handle when the process is dead is the right behavior. That's why you can use functions such as GetProcessTimes after the process is dead to find out about the ExitTime of the process
Lifetime of process handle
The process handle lifetime is independent of the process lifetime. Until you CloseHandle the process object will continue to exist and be useable and its process ID will not be reused.
Alternative to unmanaged APIs

As an alternative to using this unmanaged API in managed code, why not use the Systems.Diagnostics.Process class. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process_methods.aspx for more details about this class

vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function OpenProcess(ByVal access As Integer, ByVal inherit As Boolean, ByVal processId As Integer) As SafeProcessHandle
End Function
createProcess and openProcess
i add create a process which is success.And I keep the new process ID. Then I start another thread to monitor the process by OpenProcess which take the kept process ID as one of the arguement.

The thing is: I kill the process in Task Manager, obviously because I know the process ID. The Openprocess still return an valid HANDLE.