使C ++程序以交互方式将输入输出传递给Windows命令提示符 [英] Make c++ program to pass input output to windows command prommpt interactively

查看:409
本文介绍了使C ++程序以交互方式将输入输出传递给Windows命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的程序,以并行方式启动cmd.exe并将用户的输入作为命令,然后将其传递给cmd.exe,在执行后,我的程序应从cmd.exe中获取输出,并向用户显示.基本上是命令提示符的界面.

I want to make a simple program that starts a cmd.exe parallely and takes input from the user as a command, which is then passed to the cmd.exe, after execution my program should take the output from cmd.exe and display it to the user. Basically an interface to a command prompt.

我不想使用诸如system()之类的方法,因为它们每次都会启动cmd的新实例,而我却无法运行cd之类的命令.

I don't want to use methods like system() as they start a new instance of cmd every time and I can't run commands like cd.

我使用以下代码尝试了该代码,我可以使用该代码生成cmd并显示初始行(版权....),但是传递命令只是再次返回同一行.

I tried it with the following code with which I am able to spawn a cmd and show initial line (copyright....), but passing commands simply returns the same line again.

#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;

DWORD WINAPI exec(LPVOID inputP){

char* input=(char*) inputP;
HANDLE stdinRd, stdinWr, stdoutRd, stdoutWr;
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, true};
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD stuff;
char buff[1000];


//Create the main transfer pipe
if(!CreatePipe(&stdinRd, &stdinWr, &sa, 0) || !CreatePipe(&stdoutRd,&stdoutWr, &sa, 0)) {
cout<<"Pipe creation failed"<<endl;
}

//Get Process Startup Info
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
si.hStdOutput = stdoutWr;
si.hStdError = stdoutWr;                                                                                               
si.hStdInput = stdinRd;

//Create the CMD Shell using the process startup info above

if(!CreateProcess("C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
cout<<"Error Spawning Command Prompt."<<endl;
}


//Main while(1) Loop
while(1) 
{
Sleep(100);
//Check if cmd.exe has not stoped
GetExitCodeProcess(pi.hProcess, &stuff);
//Stop the while loop if not active
if(stuff != STILL_ACTIVE) break;

//Copy Data from buffer to pipe and vise versa
PeekNamedPipe(stdoutRd, NULL, 0, NULL, &stuff, NULL);

ZeroMemory(buff, sizeof(buff));

//Read Console Output
ReadFile(stdoutRd, buff, 1000, &stuff, NULL);
//output 
cout<<buff<<endl;


//Read data from stream and pipe it to cmd.exe
WriteFile(stdinWr, input, strlen(input), &stuff, NULL);

}

return 0;
}
int main() {
while(1){
char a[100];
cin>>a;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)exec, (LPVOID)a, 0, NULL);
}
}

推荐答案

发现我的问题,这很愚蠢.只需传递一个换行符,以便cmd将数据解释为命令,即

Found my problem, was quite silly. Just need to pass a new line character so that cmd interprets the data as a command, i.e.,

cin>>a;
strcat(a,"\n");

并且显然只通过一次调用线程并通过全局变量传递参数来创建cmd的单个实例.

and obviously make a single instance of cmd by calling the thread only once and passing parameters through global variables.

这篇关于使C ++程序以交互方式将输入输出传递给Windows命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆