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

OpenThread (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError = true)]
static extern Microsoft.Win32.SafeHandles.SafeAccessTokenHandle OpenThread(
   ThreadAccess dwDesiredAccess,
   bool bInheritHandle,
   uint dwThreadId
   );

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle,
   uint dwThreadId);

Boo Signature:

[DllImport("kernel32.dll")]
def OpenThread(dwDesiredAccess as int, bInheritHandle as bool, dwThreadId as int) as IntPtr:
     pass

User-Defined Types:

  [Flags]
  public enum    ThreadAccess : int
  {
    TERMINATE           = (0x0001)  ,
    SUSPEND_RESUME      = (0x0002)  ,
    GET_CONTEXT         = (0x0008)  ,
    SET_CONTEXT         = (0x0010)  ,
    SET_INFORMATION     = (0x0020)  ,
    QUERY_INFORMATION       = (0x0040)  ,
    SET_THREAD_TOKEN    = (0x0080)  ,
    IMPERSONATE         = (0x0100)  ,
    DIRECT_IMPERSONATION    = (0x0200)
  }
static extern Microsoft.Win32.SafeHandles.SafeAccessTokenHandle OpenThread(
   ThreadAccess dwDesiredAccess,
   bool bInheritHandle,
   uint dwThreadId
   );

Notes:

None.

Tips & Tricks:

Use the SafeAccessTokenHandle version where possible, as it will ensure proper disposal of the handle.

If you use the IntPtr version, use a try/finally block to ensure you call CloseHandle

Sample Code:

  private static void SuspendProcess(int pid)
[DllImport("kernel32.dll")]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle,
   uint dwThreadId);

User-Defined Types:

  [Flags]
  public enum    ThreadAccess : int
  {
    TERMINATE           = (0x0001)  ,
    SUSPEND_RESUME      = (0x0002)  ,
    GET_CONTEXT         = (0x0008)  ,
    SET_CONTEXT         = (0x0010)  ,
    SET_INFORMATION     = (0x0020)  ,
    QUERY_INFORMATION       = (0x0040)  ,
    SET_THREAD_TOKEN    = (0x0080)  ,
    IMPERSONATE         = (0x0100)  ,
    DIRECT_IMPERSONATION    = (0x0200)
  }

Notes:

None.

Tips & Tricks:

Use the SafeAccessTokenHandle version where possible, as it will ensure proper disposal of the handle.

If you use the IntPtr version, use a try/finally block to ensure you call CloseHandle

Sample Code:

  private static void SuspendProcess(int pid)
  {
    var process = Process.GetProcessById(pid);
    if (process.ProcessName == string.Empty)
    {
      return;
    }
    foreach (ProcessThread thread in process.Threads)
    {
      using (var handle =
           OpenThread(
         ThreadAccess.SUSPEND_RESUME,
         false,
         (uint)thread.IdThreadAccess)
        ) // using block ensures finalizer closes handle
      {
        if (!handle.IsInvalid)
        {
          SuspendThread(pOpenThread);
        }
      }
    }
  }

Alternative Managed API:

Do you know one? Please contribute it!

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