Code:
#include <windows.h>
#include <stdio.h>
#define MAX_BUFFER_SIZE 512
int EmulateCommandPrompt(LPSTR cmdline)
{
STARTUPINFO sti = { 0 };
SECURITY_ATTRIBUTES sats = { 0 };
PROCESS_INFORMATION pi = { 0 };
HANDLE pipin_w, pipin_r, pipout_w, pipout_r;
BYTE buffer[MAX_BUFFER_SIZE];
DWORD writ, excode, read, available;
int ret = 0;
pipin_w = pipin_r = pipout_w = pipout_r = NULL;
for(;;)
{
//set SECURITY_ATTRIBUTES struct fields
sats.nLength = sizeof(sats);
sats.bInheritHandle = TRUE;
sats.lpSecurityDescriptor = NULL;
//create child's stdout pipes
if(!CreatePipe(&pipout_r, &pipout_w, &sats, 0)) break;
//and its stdin pipes
if(!CreatePipe(&pipin_r, &pipin_w, &sats, 0)) break;
printf("Created pipes\n");
//now set STARTUPINFO struct fields (from the child's point of view)
sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
sti.wShowWindow = SW_HIDE;
sti.hStdInput = pipin_r;
sti.hStdOutput = pipout_w;
sti.hStdError = pipout_w;
//create the process...
if(!CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE,
0, NULL, NULL, &sti, &pi)) break;
printf("Created process (%s)\n", cmdline);
//now have a continuous loop to get and recieve info
for(;;)
{
//make sure process is still running
GetExitCodeProcess(pi.hProcess, &excode);
if(excode != STILL_ACTIVE) break;
//printf("Process still running\n");
//give it time to set up/react
Sleep(50);
//now check to see if process has anything to say
if(!PeekNamedPipe(pipout_r, buffer,
sizeof(buffer), &read, &available, NULL)) ret = 10;
//printf("Peeked\n");
//is there anything to be read in the pipe?
if(read)
{
do
{
ZeroMemory(buffer, sizeof(buffer));
//read it and print to stdout
if(!ReadFile(pipout_r, buffer, sizeof(buffer), &read, NULL) || !read) ret = 7;
buffer[read] = 0;
fprintf(stdout, "%s", buffer);
if(ret) break;
}
while(read >= sizeof(buffer));
}
//make sure we didn't run into any errors
if(!ret)
{
//get info and write it to pipe
ZeroMemory(buffer, sizeof(buffer));
fgets(buffer, sizeof(buffer), stdin);
if(!strnicmp(buffer, "exit", 4)) ret = 12;
if(!WriteFile(pipin_w, buffer, strlen(buffer), &writ, NULL)) ret = 8;
}
if(ret) break;
}
break;
}
//clean up any unfinished business
if(pipin_w != NULL) CloseHandle(pipin_w);
if(pipin_r != NULL) CloseHandle(pipin_r);
if(pipout_w != NULL) CloseHandle(pipout_w);
if(pipout_r != NULL) CloseHandle(pipout_r);
if(pi.hProcess != NULL) CloseHandle(pi.hProcess);
if(pi.hThread != NULL) CloseHandle(pi.hThread);
return ret;
}
int main(int argc, char *argv[])
{
EmulateCommandPrompt(NULL);
return 0;
}