Jump to content

Windows Programming/Programming CMD

From Wikibooks, open books for an open world

This is an old revision of this page, as edited by Whiteknight (discuss | contribs) at 04:23, 21 February 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Windows XP still maintains the old throwback DOS prompt window, and fortunately, it is a good tool to use for programming and scripting a Windows workstation. This page will attempt to describe, briefly, how to program for the DOS window. This page will discuss BATCH programming using the DOS script, and it will also talk about how to interract with the prompt using C or VB.

The Prompt

The DOS prompt can be accessed by clicking on "cmd.exe" (for windows 2000/XP) or "command.exe" (for older versions of windows).

BATCH script

BATCH script is the unofficial term for the DOS scripting language. It is often called BATCH script because it is included in "batch files", with the ".BAT" file extension. Batch files are not compiled, but are instead interpreted on the fly by the command interpreter.

comments in DOS batch files are denoted either with the keyword "REM", or in more recent versions, a double colon (::).

batch script, essentially, consists of all the command-line applications that can be run, and a few small control keywords that can be used for simple control tasks. If functionality is missing in your dos prompt, you can write a new program to handle that, and use the program in your next batch script.

commands can be entered at the DOS prompt to be executed one at a time, or they can be assembled into a batch file. Batch files are essentially just text files, that contain listings of DOS commands to be executed. In a batch file, commands are separated by a newline, and therefore only one command can appear on each line. Batch scripts can also not easily receive user input from the keyboard, and without some sort of extension program cannot interact with the mouse at all.

Basic Commands

To print a string to the prompt, we use the ECHO command. ECHO will automatically convert any variable names, and will print the resulting string to the prompt. To print an empty line, use the command "ECHO.", with a period afterwards.

To print a prompt that says "Press any key to continue...", use the PAUSE command. PAUSE will hold the program until a key is pressed.

typing any command, followed by the symbols "/?" will bring up the help to that command. If you are writing a command line application, it is typical to display a help message when the user types in this symbol (or a related symbol such as "-?", "-h", "/h", or "-help");

Variables

Most the variables available in the DOS window are considered "Global Variables", although changes in the variables of a particular DOS window do not affect the values of those variables system wide. There are other special variables for use in controlling the loop structures, and for use in passing parameters to batch scripts.

To view a list of all the variables active in the current DOS window, type the command "SET". SET will display the current variables or (if you pass in the right parameters) can change the value of a particular variable or even create a new variable.

to set a variable value, we call SET as such:

SET var=value

note that there are no spaces between the "var" and the "value".

Command-Line Interfacing

let's say we want to call a program "MyProgram" from the DOS prompt. we type the following into our prompt (the .exe file extension is unnecessary):

C:\>myprogram.exe

And this will run the myprogram executable. Now, let's say we want to pass a few arguments to this program:

C:\>myprogram arg1 arg2 arg3

Now, if we go into the standard main function, we will have our argc and argv values:

int main(int argc, char *argv[])

Where:

argc = 3
argv[0] = "myprogram"
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"

This shouldn't come as a big surprise to people who have any familiarity with standard C programming. However, if we translate this to our WinMain function, we get a different value:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int iCmdShow)

we will only have one value to accept the command line:

CmdLine = "myprogram arg1 arg2 arg3".

We can also use the function GetCommandLine to retrive this string from any point in the application. If we want to parse this into a standard argv/argc pair, we can use the function CommandLineToArgvW to perform the conversion. It is important to note that CommandLineToArgvW only works on unicode strings.

When we return a value from our C program, that value gets passed to the DOS shell, and stored in a variable called "ERRORLEVEL". ERRORLEVEL is the only global variable that is not a string, and it can contain a number from 0 to 255. By convention, a value of zero means "success", while a value other then zero signifies an error.

Let's say we wanted to write a C program that returns the number of arguments passed to it. This might sound like a simple task in C, but it is difficult to accomplish in batch script:

int main(int argc, char *argv[])
{
   return argc;
}