What do the __argc and __argv mean?  and ...

Posted on 2001-07-08
Last Modified: 2011-09-20
I got a program from someone else and the main()-function looks like follow:

#include "MyService.h"

#ifndef _CONSOLE
     #include <stdlib.h>          // for declaration of __argv and __argc

#ifdef _CONSOLE
     int main( int argc, char ** argv ) {
     // non of the arguments is used when running on Win95
     int argc = __argc;
     char ** argv = __argv;
#endif     // _CONSOLE

     // create the service-object
     CMyService serv("MyService"); // if you start the program without parameter transmission

What do the __argc and __argv mean?

Besides, if I start the program using parameter transmission like this,
     CMyService serv(argv[2]);

then it will show a memory access error message.

Question by:chenwei
LVL 32

Expert Comment

ID: 6262266
__argc is a input variable to WinMain that has a count of the command line parameters.

__argv is another input variable to WinMain that is an array of the command parameters themselves.
LVL 10

Expert Comment

ID: 6262288
char buffer[200];

for(int i=0;i<__argc;i++)
  sprintf(buffer,"%i : %s\n",i,__argv[i]);

pass some command line args to your app
LVL 22

Expert Comment

ID: 6262291
In VC you can create 2 main types of program.  You can create a console program that has the standard C++ entry point procedure like

int main(int argc, char ** argv)

This sort of program will run in a console window--like a DOS program.  This sort of program is the best to use whe writtin standard C++ programs (like you might find in a C++ programming book).  

But VC also supports windows GUI programs.   These programs have a graphical interface, that is, they produce and manipulate graphical windows.  These programs have a non-standard entry point procedure (one that is standard for windows programs, but that is not standard for C++ programs.  That is, all windows GUI programs use it (mostly), but C++ programs not on windows can't use it) Like


The code you have was written so that it could be compiled as either a console program or as a GUI program.  i.e. it uses conditional compilation (#if) to effectively give the program one entry point or the other.

Now to get to your question.

>> What do the __argc and __argv mean?

The current C++ library used by VC defines these in <stdlib.h>.  It defines them as global variables that record the parameters passed to the C++ program.   These are NOT standard definitions.  that is they are not pat of the standard C++ library.  if you try to use them on other compilers you will probably find them undefined.  They were defines by the authors of the Vc library to help them process parameters to VC programs.   This means that these are not very safe to use.  the never version of the library might not use them.  Or it might call them a different name.   Basially these are variables that another programmer (one that wrote VC's library) has created to help do a job and your program is using them, and making the assumption that they will always be availble and will always have the same name and same function.  That is a bad idea.

>> __argc is a input variable to WinMain that has a count of the command line parameters.
This are not paameters to WinMain.  They are actually #defines for global variable.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 6262637
Thanks for the answer to my question on __argc and __argv.

How about my another problem? I.e. when I instantiate the class CMyService with a predefined string, i.e. when I call it with

it runs. But when I call it with a on the fly string, i.e. when I call it with
then I will get a memory access error message.
LVL 22

Expert Comment

ID: 6262706
What is argv[2]?   Did you test to make sure it is valid?

Remember that C/C++ arrays are 0 based.  argv[0] is the program name (and path).   argv[1] is the first parameter to the program.  argv[2] is the second parameter to the program.

So to pass argv[2] you must have specified 2 parameters to the program.  That means that argc must be 3.   If argc is less than 3, then you may not safely use argv[2].

Author Comment

ID: 6262787
Sure. I start the program as follow:
  myservice.exe -i MyService

You see, the "myservice.exe" is argv[0], the "-i" is argv[1] and "MyService" is argv[2].

LVL 22

Accepted Solution

nietod earned 20 total points
ID: 6262820
Do you check this with the debugger?

Is this for a console program or a GUI program?

Author Comment

ID: 6263075
Sorry, I have to explain a little bit in detail. I've checked with debugger. There is no problem. My program is a Windows NT system service. I have to install it first, i.e. call myservice.exe -i. After the installation, I open the SCM, System Control Manager, and click the "START" button to start it. The memory access error ocuuse when I  start the service. Maybe my question is out of this catagary. If yes, I will put my questions anywhere and give you the points.


Author Comment

ID: 6268931
Sorry for giving you the points so late.
LVL 22

Expert Comment

ID: 6269045
Sorry for not responding earlier.  I didn't get notification.  EE is having e-mail notification problems.  if you don't get a response from an expert in a day or so, just post a dummy comment to try to get another e-mail notification to thyem.

>> The memory access error ocuuse when I  start the service
You might try the windows programming topic area.  I don't know much about services.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question