/// <summary> /// Sets the context for the specified thread. /// </summary> /// <param name="threadHandle">A handle to the thread whose context is to be set.</param> /// <param name="context">A pointer to a <see cref="ThreadContext"/> structure that contains the context to be set in the specified thread.</param> public static void SetThreadContext(SafeMemoryHandle threadHandle, ThreadContext context) { // Check if the handle is valid HandleManipulator.ValidateAsArgument(threadHandle, "threadHandle"); // Set the thread context if(!NativeMethods.SetThreadContext(threadHandle, ref context)) throw new Win32Exception("Couldn't set the thread context."); }
/// <summary> /// Retrieves the context of the specified thread. /// </summary> /// <param name="threadHandle">A handle to the thread whose context is to be retrieved.</param> /// <param name="contextFlags">Determines which registers are returned or set.</param> /// <returns>A <see cref="ThreadContext"/> structure that receives the appropriate context of the specified thread.</returns> public static ThreadContext GetThreadContext(SafeMemoryHandle threadHandle, ThreadContextFlags contextFlags = ThreadContextFlags.Full) { // Check if the handle is valid HandleManipulator.ValidateAsArgument(threadHandle, "threadHandle"); // Allocate a thread context structure var context = new ThreadContext {ContextFlags = contextFlags}; // Set the context flag // Get the thread context if (NativeMethods.GetThreadContext(threadHandle, ref context)) return context; // Else couldn't get the thread context, throws an exception throw new Win32Exception("Couldn't get the thread context."); }
public static extern bool GetThreadContext(SafeMemoryHandle hThread, ref ThreadContext lpContext);