Another example of passing an argument to a program



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: none

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged.

To do: Another example of passing an argument to a program

To show: Playing with Windows processes





#include <windows.h>

#include <stdio.h>

#include <process.h>


/* Environment for exec?e */

char *my_env[] =


// Just as an example here: NAME=value






int main(int ac, char* av[])


int ret;

// An array pointers...

char *args[3];

/* Arguments for _execv? */

args[0] = av[1];

args[1] = "/D";

args[2] = NULL;


// If command line arguments not properly supplied...

if (ac != 2)


fprintf(stderr, "Usage: %s <program_name>.\n", av[0]);

return 0;



// We are going to run "mem /D" command, passing the /D....

if ((ret = _execve(av[1], args, my_env)) == -1)

perror("perror says _execve() failed");

return 0;



Output example:

(This program run at the command prompt)


F:\vc2005project\crtexecprog\debug>crtexecprog C:\windows\system32\mem


Address Name Size Type

------- -------- ------ ------

000000 000400 Interrupt Vector

000400 000100 ROM Communication Area

000500 000200 DOS Communication Area

000700 IO 000370 System Data

CON System Device Driver

AUX System Device Driver

PRN System Device Driver

CLOCK$ System Device Driver

COM1 System Device Driver

LPT1 System Device Driver

LPT2 System Device Driver

LPT3 System Device Driver

COM2 System Device Driver

COM3 System Device Driver

COM4 System Device Driver

000A70 MSDOS 001690 System Data

002100 IO 001FF0 System Data

KBD 000CE0 System Program


XMSXXXX0 Installed Device Driver

000490 FILES=

000090 FCBS=


0007D0 STACKS=

004100 COMMAND 000A20 Program

004B30 MSDOS 000070 -- Free --

004BB0 COMMAND 000180 Environment

004D40 DOSX 0087A0 Program

00D4F0 MEM 0000C0 Environment

00D5C0 MEM 0174E0 Program

024AB0 MSDOS 07B530 -- Free --

09FFF0 SYSTEM 038000 System Program

0D8000 IO 003100 System Data

MOUSE 0030F0 System Program

0DB110 MSDOS 0000A0 -- Free --

0DB1C0 MSCDEXNT 0001D0 Program

0DB3A0 REDIR 000A70 Program

0DBE20 DOSX 000080 Data

0DBEB0 MSDOS 004140 -- Free --


655360 bytes total conventional memory

655360 bytes available to MS-DOS

600608 largest executable program size

1048576 bytes total contiguous extended memory

0 bytes available contiguous extended memory

941056 bytes available XMS memory

MS-DOS resident in High Memory Area



C and C++ Programming Resources | C & C++ Code Example Index