views:

74

answers:

4

I never studied OSes, so forgive me if this sounds basic or silly, but I'm curious about if one could replace the cmd prompt in Windows. I am NOT asking for programs that actually do this, as I've looked around and not really seen any.

What I'm asking is 1) if it's actually possible to write an entirely new program that would behave like cmd and 2) if one could then replace cmd with it, assuming it could perform at least all the jobs that cmd does.

Because it seems like even programs that claim to be an upgrade to it (cygwin, powershell, etc) are actually running in that same little black window. Maybe I'm just not fully understanding how cmd fits into windows as a whole, or how something like bash actually connects into linux.

Anyone care to steer me in the right direction?

A: 

CMD.EXE is just a program. There's nothing special about it.

John Saunders
So, what's the problem with this answer?
John Saunders
I didn't downvote you (in fact I upped to balance it out) but I'm guessing it was seen as too terse to be very helpful. I actually do appreciate the sentiment that it is just a program, no special system hooks or magic going on with it.
CodexArcanum
+3  A: 

"That same little black window" is a feature of Windows's "console" subsystem. Console-mode apps get that window pretty much for free, and have it opened for them (either by Windows itself or by the runtime libs, i forget which) when they're started from Windows rather than from the command line. (Console-mode programs run from an existing console tend to borrow the console of the parent process.)

The fact that console apps look alike doesn't necessarily mean they're all run from cmd.exe (although there's often a batch file that starts them, they can be started on their own), but that they all use the same subsystem and the same features.

With all that said, it's quite possible to write a replacement for cmd.exe. It's just a console app. The catch is making it compatible enough to run batch files (lest existing apps break when they expect to use cmd.exe), and still having enough flexibility to add whatever you want to add to a shell.

cHao
Though I appreciate all the answers, I feel like this one offers the most comprehensive explanation. Thank you!
CodexArcanum
+2  A: 

This might help you lead on alternative command shells that can be used on windows :) CMD.EXE is just a program that provide CLI interface. There are in fact good alternatives.

pyfunc
Nice, some good links in there, and I always appreciate cross-SO links because it helps make the site more useful.
CodexArcanum
+1  A: 

you could write an application that allows you to create and execute batch (.bat) files.

for example, let's say you use C# as your language. you can open a text stream to create the (.bat) files. then, you can execute them like this:

ProcessStartInfo CmdReplacement = new System.Diagnostics.ProcessStartInfo();

//choose a name and then arguments that you want to send to the command prompt
CmdReplacement.FileName = @"C:\testfile.bat";
CmdReplacement.Arguments = "1 2 3 4";

//if you use this property it will prevent a console window from popping up
pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//create new process and set starting information
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = CmdReplacement;


//set this to tell you when the process has completed
p.EnableRaisingEvents = true;

p.Start();

//wait until the process has completed
while(!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}

//check to see what the exit code was
if(p.ExitCode != 0)
{
//some error occurred
}
transmogrify
As a C# developer, I greatly appreciate the code. That'd be a nice jump off point to writing a C#-based console/terminal/whatever. Isn't this basically just loading the .bat file into the system default (cmd for 99%) and then running it, but hidden, and capturing the output?
CodexArcanum