#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?
最佳答案
您应该使用 processInfo.hThread
,因为它是新进程主线程的句柄。 processInfo.hProcess
是进程句柄,不是线程句柄。
至于 GetLastError()
返回 ERROR_FILE_NOT_FOUND
,大概是其他人调用了调用 SetLastError(ERROR_FILE_NOT_FOUND)
的 API。来自 GetLastError()
的文档:
Return value
The return value is the calling thread's last-error code.
The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code. Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.
关于c++ - GetThreadContext 返回错误 6,句柄无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7480802/