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)); }
/// <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)); }