GetThreadContext returning error 6, Invalid Handle?
#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;
}
开发者_StackOverflow 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?
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()
returning ERROR_FILE_NOT_FOUND
, presumably someone else called an API that called SetLastError(ERROR_FILE_NOT_FOUND)
. From the documentation of 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.
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.
精彩评论