Your lab assignment is to write a simple command shell of your own. A command shell is basically a simple console application which loops until the user indicates they have finished entering commands. When the user enters a command, the shell creates a new process to execute the command for the user, waits for the command to finish, and then prompts the user to enter their next command. Your shell should be able to execute the following commands:
User Input Command Executed
c C:\\ WINDOWS\\system32\\calc.exe
i C:\\ WINDOWS\\system32\\ipconfig.exe
n C:\\ WINDOWS\\system32\\netstat.exe
p C:\\ WINDOWS\\system32\\ping.exe
s C:\\ WINDOWS\\system32\\systeminfo.exe
t C:\\ WINDOWS\\system32\\tracert.exe
e terminate or exit the main program
For the ping and tracert commands, you should provide www.yahoo.com as an argument to the command.
Your process must use CreateProcess to create the command processes. You must also use WaitForSingleObject so that your command shell waits until the child process completes before prompting for more user input.
My questions:
1. How do add the user input capability to the program?? For example "c" for C:\\ WINDOWS\\system32\\calc.exe??
Below is what I have so far...
// Process_Example.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int main( int argc, char argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
BOOL ret;
GetStartupInfo(&si);
// Child processes
ret = CreateProcess( NULL, // No module name (use command line).
(c ("C:\\WINDOWS\\system32\\calc.exe")), // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
ret = CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\ipconfig.exe", // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
ret = CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\netstat.exe", // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
ret = CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\ping.exe", // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
ret = CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\systeminfo.exe", // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
ret = CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\tracert.exe", // executable file name & arguments
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Address of STARTUPINFO structure.
&pi ); // Address of PROCESS_INFORMATION structure.
if(ret == false)
{
cout << "Error creating process: " << GetLastError() << endl;
return -1;
}
// Wait until child process exits – forever if necessary.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hThread );
CloseHandle( pi.hProcess );
VOID WINAPI ExitProcess(
__in UINT uExitCode
);
}
This post has been edited by volvera215: 08 January 2012 - 05:38 PM

New Topic/Question
Reply




MultiQuote




|