从cmd.exeCreatProcess输出命令时,出现“更多?”为什么以及如何摆脱它?

问题描述

我对cmd.exe的第二个输出有问题。 我使用:CreatePipe()链接进程; CreateProcess()创建运行“ cmd.exe”的进程。

问题是我无法从cmd.exe获得正确的输出

#include <iostream>
#include <windows.h>

using namespace std;


#define deffBuffSize 4096


int funCo() {

    HANDLE writetoCL,readFromCL,writetoProcess,readFromProcess;
    char lpCmdLine[] = "cmd.exe",lpCL[512];
    memset(lpCL,'\0',512);
    if (GetEnvironmentvariableA("COMSPEC",lpCL,512) == 0) {
        cout << "GetEnvironmentvariableA error :: " << GetLastError() << endl;
    }
    cout << lpCL << endl;
    STARTUPINFOA siA;
    PROCESS_informatION piApp;
    Security_ATTRIBUTES secAttr;
    ZeroMemory(&secAttr,sizeof(Security_ATTRIBUTES));
    ZeroMemory(&siA,sizeof(STARTUPINFOA));
    secAttr.nLength = sizeof(Security_ATTRIBUTES);
    secAttr.lpSecurityDescriptor = NULL;
    secAttr.bInheritHandle = TRUE;

    if (CreatePipe(&readFromCL,&writetoProcess,&secAttr,0) == 0) {
        cout << "Create pipe error :: " << GetLastError() << endl;
        return 1;
    }
    if (!SetHandleinformation(readFromCL,HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT)) {
        cout << "SetHandleinformation error :: " << GetLastError() << endl;
    }

    if (CreatePipe(&readFromProcess,&writetoCL,0) == 0) {
        cout << "Create pipe error :: " << GetLastError() << endl;
        return 1;
    }
    if (!SetHandleinformation(writetoCL,HANDLE_FLAG_INHERIT)) {
        cout << "SetHandleinformation error :: " << GetLastError() << endl;
    }

    siA.cb = sizeof(STARTUPINFOA);
    siA.hStdInput = readFromProcess;
    siA.hStdOutput = writetoProcess;
    siA.hStdError = writetoProcess;
    siA.dwFlags = STARTF_USESTDHANDLES;


    if (CreateProcessA(lpCL,NULL,TRUE,CREATE_NO_WINDOW,&siA,&piApp) == 0) {
        cout << "CreateProcessA error :: " << GetLastError() << endl;
        return 1;
    }
    
    CloseHandle(readFromProcess);
    CloseHandle(writetoProcess);

    Sleep(2000);
    DWORD dRead = 0,dWrite = 0;
    char chBuff[deffBuffSize];
    memset(chBuff,deffBuffSize);
    if (ReadFile(readFromCL,chBuff,deffBuffSize,&dRead,NULL) == FALSE) {
        cout << "ReadFile error :: " << GetLastError() << endl;
        return 1;
    }
    cout << chBuff;

    for (;;) {
        memset(chBuff,deffBuffSize);
        fgets(chBuff,stdin);
        if (WriteFile(writetoCL,sizeof(chBuff),&dWrite,NULL) == FALSE) {
            cout << "WriteFile error :: " << GetLastError() << endl;
            return 1;
        }
        Sleep(2000);
        memset(chBuff,deffBuffSize);
        if (ReadFile(readFromCL,NULL) == FALSE) {
            cout << "ReadFile error :: " << GetLastError() << endl;
            return 1;
        }
        cout << chBuff;

    }

    CloseHandle(writetoCL);
    CloseHandle(readFromCL);
    CloseHandle(piApp.hProcess);
    CloseHandle(piApp.hThread);
    return 0;
}

int main() {
    int result = 0;
    result = funCo();
    cout << endl;
    system("pause");
    return result;
}

Console output of parent process

如果输出为“更多?”按ENTER,然后我们将收到错误消息ERROR_broKEN_PIPE(109)-管道已结束。

Error screenshot

我如何理解“更多”?如果要显示下一页信息,则写。

但是如何解决这个“更多?”

请帮助,我将非常感谢您。 =)

以下部分与评论有关:

自动滚动未禁用

cmd -> Properties

我在写入cmd.exe时尝试使用静态命令

但是在执行此操作后,我发现自己陷入了“僵局”

Sleep(2000);
    DWORD dRead = 0,NULL) == FALSE) {
        cout << "ReadFile error :: " << GetLastError() << endl;
        return 1;
    }
    cout << chBuff;
    char outMes[] = "ipconfig\0";

    for (;;) {
        /*memset(chBuff,stdin);*/
        
        if (WriteFile(writetoCL,outMes,sizeof(outMes),NULL) == FALSE) {
            cout << "WriteFile error :: " << GetLastError() << endl;
            return 1;
        }
        cout << "WriteFile :: " << dWrite << endl;
        Sleep(2000);
        memset(chBuff,NULL) == FALSE) {
            cout << "ReadFile error :: " << GetLastError() << endl;
            return 1;
        }
        cout << chBuff;

    }

解决方法

整个错误是我:

  1. 使用了“ \ 0”
  2. WriteFile(writeToCL,outMes,sizeof(outMes)-1,&dWrite,NULL) 写了两个字符“ \ 0”

该程序不断输入和接收有关ipconfig的信息

命令行使用如下命令:[i] [p] [c] [o] [n] [f] [i] [g] [\ n] [\ 0]

char outMes[] = "ipconfig\n";

for (;;) {
if (WriteFile(writeToCL,outMes,sizeof(outMes) - 1,&dWrite,NULL) == FALSE) {
cout << GetLastError() << " :: error WriteFile" << endl;
return 1;
}
Sleep(2000);
memset(chBuff,'\0',deffBuffSize);
if (ReadFile(readFromCL,chBuff,deffBuffSize,&dRead,NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;

}

P.S:使用strlen确定字符串的长度。 代替sizeof()