1 Replies - 5030 Views - Last Post: 09 January 2012 - 04:22 AM Rate Topic: -----

#1 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Command Shell on Windows in C++

Posted 08 January 2012 - 05:36 PM

I am new to this aspect of programming and I need some guidance on my assignment this week. Below is the assignment:

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


Is This A Good Question/Topic? 0
  • +

Replies To: Command Shell on Windows in C++

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Command Shell on Windows in C++

Posted 09 January 2012 - 04:22 AM

Basic Input/Output C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1