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.
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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