• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4920
  • Last Modified:

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

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.

1 Solution
__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.
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
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.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

chenweiAuthor Commented:
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.
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].
chenweiAuthor Commented:
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].

Do you check this with the debugger?

Is this for a console program or a GUI program?
chenweiAuthor Commented:
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.

chenweiAuthor Commented:
Sorry for giving you the points so late.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now