Command line arguments

It is commonly known that to output the command line arguments for a standard C++ program we could write a piece of code like that:

int main(int argc, char** argv)
for (int i = 0; i < argc; ++i)
std::cout << "Argument " << i << ": " << argv[i] << std::endl;


Where the first argument is the name of the program itself.

To achieve something similar in C++/CLI we write this:

int main(cli::array^ args)
for (int i = 0; i < args->Length; ++i)
System::Console::WriteLine("Argument {0}: {1}", i, args[i]);


I wrote "similar" not for the fact that we deal with System::Strings instead of raw arrays of chars - this is just an implementation details, after all - but because the program name is not in the argument lists.

To be a bit more pedantic, the managed version of the main function for a C++/CLI program accepts in input a managed array of managed System::String. The environment puts in this array any argument passed to the program.
This post is based on my reading on the book "Expert C++/CLI: .NET for Visual C++ Programmers" by Marcus Heege, APress.

No comments:

Post a Comment