GetThreadContext Function Home
GetThreadContext Function

Retrieves the context of the specified thread.

A 64-bit application can retrieve the context of a WOW64 thread using the Wow64GetThreadContext function.

Syntax

BOOL WINAPI GetThreadContext(
  __in     HANDLE hThread,
  __inout  LPCONTEXT lpContext
);


Parameters

hThread [in]

A handle to the thread whose context is to be retrieved. The handle must have THREAD_GET_CONTEXT access to the thread. For more information, see Thread Security and Access Rights.

WOW64:  The handle must also have THREAD_QUERY_INFORMATION access.
lpContext [in, out]

A pointer to a CONTEXT structure that receives the appropriate context of the specified thread. The value of the ContextFlags member of this structure specifies which portions of a thread's context are retrieved. The CONTEXT structure is highly processor specific. Refer to the WinNt.h header file for processor-specific definitions of this structures and any alignment requirements.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

This function is used to retrieve the thread context of the specified thread. The function retrieves a selective context based on the value of the ContextFlags member of the context structure. The thread identified by the hThread parameter is typically being debugged, but the function can also operate when the thread is not being debugged.

You cannot get a valid context for a running thread. Use the SuspendThread function to suspend the thread before calling GetThreadContext.

If you call GetThreadContext for the current thread, the function returns successfully; however, the context returned is not valid.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

CONTEXT
Debugging Functions
SetThreadContext
SuspendThread

Send comments about this topic to Microsoft

Build date: 11/12/2009

Community Content

Get and Set Thread Context
Added by:spyder
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <assert.h>

void mythreadfunc(void *data)
{
...

_exitthread();
}

void changeThreadState()
{
HANDLE thread = (HANDLE)_beginthread( mythreadfunc, 0, NULL );
CONTEXT context;
BOOL success;

SuspendThread(thread);

// get context
context.ContextFlags = (CONTEXT_FULL | CONTEXT_CONTROL);
success = GetThreadContext(thread, &context);
assert(success);
printf( "eax=%08X, ebx=%08X, ecx=%08X\n",
context.Eax, context.Ebx, context.Ecx );

// change context (dangerous, can crash program)
context.Eax = 0x1234BBBB;
context.Ecx = 0x2468ABCD;
success = SetThreadContext(thread, &context);
assert(success);

ResumeThread(thread);

// the resumed thread should see different values of eax, and ecx
}
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View