Binarysharp.MemoryManagement.Threading.ThreadCore.OpenThread C# (CSharp) Method

OpenThread() public static method

Opens an existing thread object.
public static OpenThread ( ThreadAccessFlags accessFlags, int threadId ) : SafeMemoryHandle
accessFlags ThreadAccessFlags The access to the thread object.
threadId int The identifier of the thread to be opened.
return Binarysharp.MemoryManagement.Native.SafeMemoryHandle
        public static SafeMemoryHandle OpenThread(ThreadAccessFlags accessFlags, int threadId)
        {
            // Open the thread
            var ret = NativeMethods.OpenThread(accessFlags, false, threadId);

            // If the thread was opened
            if (!ret.IsClosed && !ret.IsInvalid)
                return ret;

            // Else couldn't open the thread, throws an exception
            throw new Win32Exception(string.Format("Couldn't open the thread #{0}.", threadId));
        }

Usage Example

 /// <summary>
 ///     Initializes a new instance of the <see cref="RemoteThread" /> class.
 /// </summary>
 /// <param name="memorySharp">The reference of the <see cref="MemoryManagement.MemorySharp" /> object.</param>
 /// <param name="thread">The native <see cref="ProcessThread" /> object.</param>
 internal RemoteThread(MemoryBase memorySharp, ProcessThread thread)
 {
     // Save the parameters
     MemorySharp = memorySharp;
     Native      = thread;
     // Save the thread id
     Id = thread.Id;
     // Open the thread
     Handle = ThreadCore.OpenThread(ThreadAccessFlags.AllAccess, Id);
     // Initialize the TEB
     Teb = new ManagedTeb(MemorySharp, ManagedTeb.FindTeb(Handle));
 }