Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

OpenProcess (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
   uint dwProcessId);

VB.NET Signature:

  Declare Auto Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As PROCESS_ACCESS, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Long) As IntPtr
  End Function

User-Defined Types (VB.NET):

    <Flags()> _
    Public Enum PROCESS_ACCESS As Long
    ' Specifies all possible access flags for the process object.
    PROCESS_ALL_ACCESS = &H1F0FFF

    ' Enables using the process handle in the CreateRemoteThread function
    ' to create a thread in the process.
    PROCESS_CREATE_THREAD = &H2

    ' Enables using the process handle as either the source or
    ' target process in the DuplicateHandle function to duplicate a handle.
    PROCESS_DUP_HANDLE = &H40

    ' Enables using the process handle in the GetExitCodeProcess and
    ' GetPriorityClass functions to read information from the process object.
    PROCESS_QUERY_INFORMATION = &H400

    ' Enables using the process handle in the SetPriorityClass function to
    ' set the priority class of the process.
    PROCESS_SET_INFORMATION = &H200

    ' Enables using the process handle in the TerminateProcess function to
    ' terminate the process.
    PROCESS_TERMINATE = &H1

    ' Enables using the process handle in the VirtualProtectEx and
    ' WriteProcessMemory functions to modify the virtual memory of the process.
    PROCESS_VM_OPERATION = &H8

    ' Enables using the process handle in the ReadProcessMemory function to
    ' read from the virtual memory of the process.
    PROCESS_VM_READ = &H10

    ' Enables using the process handle in the WriteProcessMemory function to
    ' write to the virtual memory of the process.
    PROCESS_VM_WRITE = &H20

    ' Enables using the process handle in any of the wait functions to wait
    ' for the process to terminate.
    SYNCHRONIZE = &H100000
    End Enum

User-Defined Types (C#):

    [Flags]
    public enum ProcessAccessFlags : uint
    {
        All = 0x001F0FFF,
        Terminate = 0x00000001,
        CreateThread = 0x00000002,
        VMOperation = 0x00000008,
        VMRead = 0x00000010,
        VMWrite = 0x00000020,
        DupHandle = 0x00000040,
        SetInformation = 0x00000200,
        QueryInformation = 0x00000400,
        Synchronize = 0x00100000
    }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        ' Get the handle of a running process
        Dim curProcess As Process = Process.GetCurrentProcess()
        hProcess = OpenProcess(PROCESS_ACCESS.PROCESS_QUERY_INFORMATION, True, curProcess.Id)

C#

    //a Variable to hold the process id
    uint ProccessID;

    //Gets the Handle To the Window (Using an Open Notepad Window For Example)

Documentation
FindWindow Pinvoke

    IntPtr hWnd = FindWindow(0, "Untitled - Notepad");

    // Gets The Windows Process ID To Use later
    GetWindowThreadProcessId(hWnd, out ProccessID);

    //Opens the Process
    IntPtr ProcessHandle = OpenProcess(ProcessAccessFlags.VMRead, false, ProccessID);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
OpenProcess on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions