当从 cmd.exe (CreatProcess) 输出命令时,我得到 "More?" 为什么以及如何摆脱它?
When outputting commands from cmd.exe (CreatProcess) I get "More?" Why and how to get rid of it?
我对 cmd.exe 的第二个输出有疑问。
我使用:CreatePipe() 来link个进程; 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, '[=12=]', 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, &secAttr, 0) == 0) {
cout << "Create pipe error :: " << GetLastError() << endl;
return 1;
}
if (!SetHandleInformation(writeToCL, HANDLE_FLAG_INHERIT, 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, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &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, '[=12=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
for (;;) {
memset(chBuff, '[=12=]', deffBuffSize);
fgets(chBuff, deffBuffSize, stdin);
if (WriteFile(writeToCL, chBuff, sizeof(chBuff), &dWrite, NULL) == FALSE) {
cout << "WriteFile error :: " << GetLastError() << endl;
return 1;
}
Sleep(2000);
memset(chBuff, '[=12=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, 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;
}
- 黄色标记是用户输入的命令。
- 红色标记 - 命令结果的输出。
如果输出是“更多?”按 ENTER,然后我们将收到一个错误 ERROR_BROKEN_PIPE (109) - 管道已结束。
如何理解“更多”?如果要显示下一个 sheet 信息,请写入。
但是如何绕过这个“更多”?
请帮忙,我将非常感谢你。 =)
以下部分与评论有关:
未禁用自动滚动
我在写入 cmd.exe
时尝试使用静态命令
但是在这个动作之后我发现自己陷入了“僵局”
Sleep(2000);
DWORD dRead = 0, dWrite = 0;
char chBuff[deffBuffSize];
memset(chBuff, '[=13=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
char outMes[] = "ipconfig[=13=]";
for (;;) {
/*memset(chBuff, '[=13=]', deffBuffSize);
fgets(chBuff, deffBuffSize, stdin);*/
if (WriteFile(writeToCL, outMes, sizeof(outMes), &dWrite, NULL) == FALSE) {
cout << "WriteFile error :: " << GetLastError() << endl;
return 1;
}
cout << "WriteFile :: " << dWrite << endl;
Sleep(2000);
memset(chBuff, '[=13=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
}
整个错误是我:
- 使用“\0”
- 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, '[=10=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
}
P.S:使用strlen判断一个字符串的长度。
而不是 sizeof()
我对 cmd.exe 的第二个输出有疑问。 我使用:CreatePipe() 来link个进程; 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, '[=12=]', 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, &secAttr, 0) == 0) {
cout << "Create pipe error :: " << GetLastError() << endl;
return 1;
}
if (!SetHandleInformation(writeToCL, HANDLE_FLAG_INHERIT, 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, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &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, '[=12=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
for (;;) {
memset(chBuff, '[=12=]', deffBuffSize);
fgets(chBuff, deffBuffSize, stdin);
if (WriteFile(writeToCL, chBuff, sizeof(chBuff), &dWrite, NULL) == FALSE) {
cout << "WriteFile error :: " << GetLastError() << endl;
return 1;
}
Sleep(2000);
memset(chBuff, '[=12=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, 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;
}
- 黄色标记是用户输入的命令。
- 红色标记 - 命令结果的输出。
如果输出是“更多?”按 ENTER,然后我们将收到一个错误 ERROR_BROKEN_PIPE (109) - 管道已结束。
如何理解“更多”?如果要显示下一个 sheet 信息,请写入。
但是如何绕过这个“更多”?
请帮忙,我将非常感谢你。 =)
以下部分与评论有关:
未禁用自动滚动
我在写入 cmd.exe
时尝试使用静态命令但是在这个动作之后我发现自己陷入了“僵局”
Sleep(2000);
DWORD dRead = 0, dWrite = 0;
char chBuff[deffBuffSize];
memset(chBuff, '[=13=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
char outMes[] = "ipconfig[=13=]";
for (;;) {
/*memset(chBuff, '[=13=]', deffBuffSize);
fgets(chBuff, deffBuffSize, stdin);*/
if (WriteFile(writeToCL, outMes, sizeof(outMes), &dWrite, NULL) == FALSE) {
cout << "WriteFile error :: " << GetLastError() << endl;
return 1;
}
cout << "WriteFile :: " << dWrite << endl;
Sleep(2000);
memset(chBuff, '[=13=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
}
整个错误是我:
- 使用“\0”
- 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, '[=10=]', deffBuffSize);
if (ReadFile(readFromCL, chBuff, deffBuffSize, &dRead, NULL) == FALSE) {
cout << "ReadFile error :: " << GetLastError() << endl;
return 1;
}
cout << chBuff;
}
P.S:使用strlen判断一个字符串的长度。 而不是 sizeof()