c++ - GetThreadContext 返回错误 6,句柄无效?

标签 c++ winapi getlasterror

#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/

相关文章:

c++ - 使 mongo-cxx-driver 找不到包括

winapi - 如何指定与 mciSendString API 一起使用的声卡

c++ - 在 catch block 中调用的 GetLastError 产生不正确的值

c++ - 检查 std::function 是否分配给 nullptr

c++ - C++ 中的 mySQL 查询

c++ - 应该如何编写 binding.gyp 以使用 OpenNI 构建 Node.js 插件?

c++ - 将参数传递给 Windows 中的窗口调用

winapi - 如何在 AppxManifest.xml 文件中为使用 Desktop Bridge 转换的 Win32 应用程序正确构建 UWP 应用程序图标(Project Centennial)

php - mongodb getLastError() php