C++ WInApi ReadFile 不读取任何内容并报告错误 109 ERROR_BROKEN_PIPE

问题描述 投票:0回答:1

我一直在尝试从 C++ 应用程序调用

adb
并读取其输出,但没有成功。
adb
存在于 PATH 上。

即使我等待,ReadFile 调用也不会读取任何内容。

GetLastError()
返回 109 ERROR_BROKEN_PIPE。

我已经尝试了有关此事的所有现有 StackOverflow 问题中提出的所有解决方案,但没有成功。

即使尝试捕获

cmd /c echo Hello World
的输出也不起作用。

代码:

#include <array>
#include <string>
#include <windows.h>

int main() {
    std::string adbConnectCommand = "adb connect localhost:5555";

    STARTUPINFO startupInfo;
    SECURITY_ATTRIBUTES secAttr;
    PROCESS_INFORMATION procInfo;

    HANDLE stdoutReadHandle = NULL;
    HANDLE stdoutWriteHandle = NULL;

    ZeroMemory(&secAttr, sizeof(secAttr));
    secAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    secAttr.bInheritHandle = TRUE;
    secAttr.lpSecurityDescriptor = NULL;

    // Create a pipe for the child process's STDOUT
    if (!CreatePipe(&stdoutReadHandle, &stdoutWriteHandle, &secAttr, 0))
        return 2; // error

    // Ensure the read handle to the pipe for STDOUT is not inherited
    if (!SetHandleInformation(stdoutReadHandle, HANDLE_FLAG_INHERIT, 0))
        return 2; // error

    ZeroMemory(&startupInfo, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);
    startupInfo.hStdError = &stdoutWriteHandle;
    startupInfo.hStdOutput = &stdoutWriteHandle;
    startupInfo.dwFlags |= STARTF_USESTDHANDLES;

    ZeroMemory(&procInfo, sizeof(procInfo));

    // Start the child process
    if (CreateProcessA(NULL,                // No module name (use command line)
        (TCHAR*)adbConnectCommand.c_str(),  // Command line
        NULL,                               // Process handle not inheritable
        NULL,                               // Thread handle not inheritable
        TRUE,                               // Set handle inheritance
        0,                                  // No creation flags
        NULL,                               // Use parent's environment block
        NULL,                               // Use parent's starting directory 
        &startupInfo,                       // Pointer to STARTUPINFO structure
        &procInfo)                          // Pointer to PROCESS_INFORMATION structure
        )
    {
        // Close the handles we don't need so we can read from stdoutReadHandle
        CloseHandle(procInfo.hProcess);
        CloseHandle(procInfo.hThread);
        CloseHandle(stdoutWriteHandle);

        std::array<char, 512> buffer;
        std::string adbConnectOutput;

        while(ReadFile(stdoutReadHandle, buffer.data(), buffer.size(), NULL, NULL))
            adbConnectOutput += buffer.data();

        CloseHandle(stdoutReadHandle);

        if (adbConnectOutput.find("connected to localhost:5555") == std::string::npos)
            return 1; // not found

        m_Connected = true;
        return 0; // success
    }

    return 2; // error
}
c++ winapi readfile createprocess
1个回答
0
投票

在此代码中:

startupInfo.hStdError = &stdoutWriteHandle;
startupInfo.hStdOutput = &stdoutWriteHandle;

您需要删除

&
,它们不属于那里,例如:

startupInfo.hStdError = stdoutWriteHandle;
startupInfo.hStdOutput = stdoutWriteHandle;
© www.soinside.com 2019 - 2024. All rights reserved.