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
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
openprocess (kernel32)
 
.
Summary

C# Signature:

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

or

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

VB.NET Signature:

<DllImport("kernel32.dll")> _
Private Shared Function OpenProcess(ByVal dwDesiredAccess As ProcessAccessFlags, <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
End Function

Boo Signature:

[DllImport("kernel32")]
static def OpenProcess(dwDesiredAccess as ProcessAccess, bInheritHandle as bool, dwProcessId as int) as IntPtr:
     pass

User-Defined Types:

ProcessAccess

Notes:

See also CloseHandle,

Tips & Tricks:

Please add some!

Flags:

C#

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

VB.NET

Enum ProcessAccessFlags As UInteger
    All = &H1F0FFF
    Terminate = &H1
    CreateThread = &H2
    VMOperation = &H8
    VMRead = &H10
    VMWrite = &H20
    DupHandle = &H40
    SetInformation = &H200
    QueryInformation = &H400
    Synchronize = &H100000
End Enum

Documentation
OpenProcess on MSDN

Sample Code (VB.NET):

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

C# Example

    using System;
    using System.Runtime.InteropServices;
    using System.Text;    
    public class ShowProcessName
    {
       [DllImport("Psapi.dll", SetLastError = true)]
    static extern bool EnumProcesses(
       [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] [In][Out]     UInt32[] processIds,
     UInt32 arraySizeBytes,
     [MarshalAs(UnmanagedType.U4)] out UInt32 bytesCopied
      );
    [Flags]
    enum ProcessAccessFlags : uint
    {
    All = 0x001F0FFF,
    Terminate = 0x00000001,
    CreateThread = 0x00000002,
    VMOperation = 0x00000008,
    VMRead = 0x00000010,
    VMWrite = 0x00000020,
    DupHandle = 0x00000040,
    SetInformation = 0x00000200,
    QueryInformation = 0x00000400,
    Synchronize = 0x00100000
    }

    [DllImport("psapi.dll")]
    static extern uint GetModuleBaseNameA(IntPtr hProcess, IntPtr hModule,         StringBuilder      lpBaseName, uint nSize);

    public ShowProcessName()
    {

    UInt32 arraySize = 120;
    UInt32 arrayBytesSize = arraySize * sizeof(UInt32);
    UInt32[] processIds = new UInt32[arraySize];
    UInt32 bytesCopied;

    EnumProcesses(processIds, arrayBytesSize, out bytesCopied);

    for (UInt32 index = 0; index < numIdsCopied; index++)
    {
        Console.WriteLine("ProcessIds[{0}] = {1}", index, processIds[index]);
        IntPtr Handle;
        Handle = OpenProcess(ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VMRead, false, (int)processIds[index]);
        if ((int)Handle <= 4)
        continue;

        StringBuilder BaseName = new StringBuilder();

        GetModuleBaseNameA(Handle, (IntPtr)0, BaseName, 100);

    }

    }

}

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
Edit This Page
Find References
Show Printable Version
Revisions