c++ - SetThreadContext x64 volatile 寄存器

标签 c++ windows multithreading x86-64 cpu-registers

我在 SetThreadContext 方面遇到问题。 我无法更改任何 volatile 寄存器(表 here )。

data.context.ContextFlags = CONTEXT_FULL;
SuspendThread(hThread);

GetThreadContext(hThread, &data.context);

...

CONTEXT* ctx = &data.context;
ctx->ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;

ctx->Rax = (DWORD64)0x1000;
ctx->Rcx = (DWORD64)-1;
ctx->Rip = (DWORD64)allocatedMemory;
ctx->R10 = (DWORD64)0x12345678;
ctx->Rbp = (DWORD64)0xFFFFFFFF;
SetThreadContext(hThread, ctx);

ResumeThread(hThread);

该代码更改了 Rip 和 Rbp 寄存器,因为它们是非 volatile 的。 GetLastError 返回 0。 使用 THREAD_SET_CONTEXT | 打开线程THREAD_GET_CONTEXT | 线程获取上下文线程暂停恢复THREAD_SET_INFORMATION 权限。

为什么我无法更改 x64 应用程序中的 volatile 寄存器?在 x86 应用程序中,我可以更改任何寄存器( volatile 与否并不重要)。

附注是的,我尝试使用 ctx->ContextFlags = CONTEXT_FULL; 调用 SetThreadContext;

最佳答案

在 64 位 Windows 中,SetThreadContext 仅设置非 volatile 寄存器。 (请参阅 WRK 中/base/ntos/ps/amd64/psctxamd64.c 中的 PspGetSetContextInternal)1

仅设置的寄存器为:Rbx、Rsp、Rbp、Rsi、Rdi、R12-R15、Xmm6-Xmm15。

关于c++ - SetThreadContext x64 volatile 寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25004311/

相关文章:

c++ - 当单击按钮隐藏位于其他类中的另一个按钮时

C++/Qt 全局热键

android - 无法使用库莱布拉

JavaScript setInterval() 不适用于 Windows CE 设备

c - Linux 多线程 C 程序<->usleep() 以每小时为间隔进行调度的准确性

java - 使用swing在java中计算和打印时间

c++ - 制作图书馆时是否应避免使用宏

c++ - C++中的蓝牙

.net - 安装后 MSI 文件复制到哪里?

multithreading - Rust 中与外部进程的多线程通信