GetThreadContext 返回错误 6,句柄无效?
#include <iostream>
#include <Windows.h>
using std::cout;
using std::endl;
using std::cin;
int main()
{
cout << "1." << GetLastError() << endl;
PROCESS_INFORMATION processInfo;
STARTUPINFOA startupInfo = {0};
CONTEXT context;
context.ContextFlags = CONTEXT_FULL;
cout << "3." << GetLastError() << endl;
if (CreateProcess((PCHAR)"rsclient.exe", NULL, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &startupInfo, &processInfo) == false) {
cout << "CreateProcess error: " << GetLastError() << endl;
}
cout << "4." << GetLastError() << endl;
if (GetThreadContext(processInfo.hProcess, &context) == false) {
cout << "GetThreadContext error:" << GetLastError() << endl;
}
return 0;
}
输出:
1.2
3.2
4.1813
GetThreadContext error:6
我可以在任务管理器中看到暂停的进程,但我收到无效句柄错误?
另外,为什么 GetLastError() 在程序开始时给出 ERROR_FILE_NOT_FOUND ?
#include <iostream>
#include <Windows.h>
using std::cout;
using std::endl;
using std::cin;
int main()
{
cout << "1." << GetLastError() << endl;
PROCESS_INFORMATION processInfo;
STARTUPINFOA startupInfo = {0};
CONTEXT context;
context.ContextFlags = CONTEXT_FULL;
cout << "3." << GetLastError() << endl;
if (CreateProcess((PCHAR)"rsclient.exe", NULL, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &startupInfo, &processInfo) == false) {
cout << "CreateProcess error: " << GetLastError() << endl;
}
cout << "4." << GetLastError() << endl;
if (GetThreadContext(processInfo.hProcess, &context) == false) {
cout << "GetThreadContext error:" << GetLastError() << endl;
}
return 0;
}
output:
1.2
3.2
4.1813
GetThreadContext error:6
I can see the suspended process in task manager yet I'm getting an invalid handle error?
Also why does GetLastError() give an ERROR_FILE_NOT_FOUND at the start of the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
processInfo.hThread
,因为它是新进程的主线程的句柄。processInfo.hProcess
是进程句柄,而不是线程句柄。至于
GetLastError()
返回ERROR_FILE_NOT_FOUND
,可能是其他人调用了一个名为SetLastError(ERROR_FILE_NOT_FOUND)
的 API。来自的文档GetLastError()
:You should use
processInfo.hThread
as that is the handle to the primary thread of the new process.processInfo.hProcess
is a process handle, not a thread handle.As for
GetLastError()
returningERROR_FILE_NOT_FOUND
, presumably someone else called an API that calledSetLastError(ERROR_FILE_NOT_FOUND)
. From the documentation ofGetLastError()
:当您使用进程 ID 作为输入调用 GetThreadContext 时,Windows 无法找到任何此类线程,因此返回 ERROR_FILE_NOT_FOUND。最好给新创建的进程的主线程,你会得到想要的结果。
As you are calling GetThreadContext with process id as input, so Windows is not able to find any such kind of thread, so returning ERROR_FILE_NOT_FOUND. Better you give the main thread of newly created process and you will get the desired result.