< Old To New C++ .Net version 8 | Main | New Visual C++ .Net Migration 1 >



Early Stages of the C++ .Net 31

(From Managed Extensions for C++ to new C++ .NET Syntax)



The code snippets used in this module if any are Visual C++ .Net 2003 dominated and if compiled using Visual C++ .Net 2005 you need to use the /clr:oldSyntax option). You may want to compare the native C++ main() and command line argument that can be found at C/C++ command line arguments page. The following are the topics available in this module.

  1. The C++ main() or Program Startup

  2. The Argument Definitions

  3. Customizing C++ Command-Line Processing

The C++ main() or Program Startup


A special function called main() is the starting point of execution for all C and C++ programs. If you are writing code that adheres to the Unicode programming model, you can use the wide-character version of main(), wmain(). The main() function is not predefined by the compiler; rather, it must be supplied in the program text. The declaration syntax for main is:

int main( );

or, optionally:

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

The following is Microsoft Specific. The declaration syntax for wmain() is as follows:

int wmain( );

or, optionally:

int wmain( int argc[ , wchar_t *argv[ ] [, wchar_t *envp[ ] ] ] );

You can also use _tmain(), which is defined in TCHAR.h. _tmain() will resolve to main unless _UNICODE is defined, in which case _tmain() will resolve to wmain(). The types for argc and argv are defined by the language. The names argc, argv, and envp are traditional, but are not required by the compiler. Alternately, the main() and wmain() functions can be declared as returning void (no return value). If you declare main() or wmain() as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main() or wmain() is declared as void, you must use the exit() function.


The Argument Definitions


The arguments in the prototype are shown below.

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

int wmain( int argc[ , wchar_t *argv[ ] [, wchar_t *envp[ ] ] ] );


This allows convenient command-line parsing of arguments and, optionally, access to environment variables. The argument definitions are as follows:





An integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.


An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked (program name), argv[1] is the first command-line argument, and so on, until argv[argc], which is always NULL. The first command-line argument is always argv[1] and the last one is argv[argc – 1].

Microsoft Specific


The envp array, which is a common extension in many UNIX® systems, is used in Microsoft C++. It is an array of strings representing the variables set in the user's environment. This array is terminated by a NULL entry. It can be declared as an array of pointers to char (char *envp[ ]) or as a pointer to pointers to char (char **envp). If your program uses wmain() instead of main(), use the wchar_t data type instead of char. The environment block passed to main() and wmain() is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv()/_wgetenv() and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change. This argument is ANSI compatible in C, but not in C++.



By convention, argv[0] is the command with which the program is invoked. However, it is possible to spawn a process using CreateProcess() and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name; use GetModuleFileName() to retrieve the executable name, and its fully-qualified path. The following example shows how to use the argc, argv, and envp arguments to main():

// command line argument

// compile with: /EHsc

#include <iostream>

#include <string.h>


using namespace std;


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


    int iNumberLines = 0;    // Default is no line numbers.


    // If /n is passed to the .exe, display numbered listing

    // of environment variables.

    if ((argc == 2) && _stricmp(argv[1], "/n" ) == 0)

         iNumberLines = 1;


    // Walk through list of strings until a NULL is encountered.

    for(int i = 0; envp[i] != NULL; ++i )



            cout<<i<<": " <<envp[i]<<"\n";



The following are the sample outputs (run at command prompt):



F:\vc2005project\cplus\debug>cplus test some argument

F:\vc2005project\cplus\debug>cplus test argument

F:\vc2005project\cplus\debug>cplus /n


0: ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS

1: APPDATA=C:\Documents and Settings\Johnny\Application Data


3: CommonProgramFiles=C:\Program Files\Common Files


5: ComSpec=C:\WINDOWS\system32\cmd.exe



8: HOMEPATH=\Documents and Settings\Johnny

9: INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\

10: LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\



13: OS=Windows_NT

14: Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Pr

ogram Files\Microsoft SQL Server\90\Tools\binn\



17: PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel



20: ProgramFiles=C:\Program Files



23: SystemDrive=C:

24: SystemRoot=C:\WINDOWS

25: TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp

26: TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp


28: USERNAME=Johnny

29: USERPROFILE=C:\Documents and Settings\Johnny

30: VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\

31: VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\

32: windir=C:\WINDOWS







Customizing C++ Command-Line Processing


This information is Microsoft Specific. If your program does not take command-line arguments, you can save a small amount of space by suppressing use of the library routine that performs command-line processing. This routine is called _setargv(). To suppress its use, define a routine that does nothing in the file containing the main() function, and name it _setargv(). The call to _setargv() is then satisfied by your definition of _setargv(), and the library version is not loaded. Similarly, if you never access the environment table through the envp argument, you can provide your own empty routine to be used in place of _setenvp(), the environment-processing routine. Just as with the _setargv() function, _setenvp() must be declared as extern "C". Your program might make calls to the spawn or exec family of routines in the C run-time library. If this is the case, you should not suppress the environment-processing routine, since this routine is used to pass an environment from the parent process to the child process. You can use wildcards, the question mark (?) and asterisk (*), to specify filename and path arguments on the command line. By default, _setargv() expands wildcards into separate strings in the argv string array. If no matches are found for the wildcard argument, the argument is passed literally.



Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9



< Old To New C++ .Net version 8 | Main | New Visual C++ .Net Migration 1 >