Opens an existing local process object.
HANDLE WINAPI OpenProcess( __in DWORD dwDesiredAccess, __in BOOL bInheritHandle, __in DWORD dwProcessId );
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.
If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
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.
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.
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.
For an example, see Taking a Snapshot and Viewing Processes.
Send comments about this topic to Microsoft
Build date: 10/8/2009
The thing is: I kill the process in Task Manager, obviously because I know the process ID. The Openprocess still return an valid HANDLE.
How can I work around this problem, if the only thing I have is a process ID which I need to check for?
[tfl - 28 05 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at
http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.frameworkAll Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
<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 SafeProcessHandleEnd Function
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] public static extern SafeProcessHandle OpenProcess(int access, bool inherit, int processId);
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