Solved

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

Posted on 2001-07-08
10
4,652 Views
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
#endif

#ifdef _CONSOLE
     int main( int argc, char ** argv ) {
#else
     int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int) {
     // 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.

Why?
0
Comment
Question by:chenwei
10 Comments
 
LVL 32

Expert Comment

by:jhance
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.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6262288
char buffer[200];

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

pass some command line args to your app
0
 
LVL 22

Expert Comment

by:nietod
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


int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)

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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chenwei
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
     CMyService("MyService");

it runs. But when I call it with a on the fly string, i.e. when I call it with
    CMyService(argv[2]);
then I will get a memory access error message.
0
 
LVL 22

Expert Comment

by:nietod
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].
0
 

Author Comment

by:chenwei
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].

0
 
LVL 22

Accepted Solution

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

Is this for a console program or a GUI program?
0
 

Author Comment

by:chenwei
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.

0
 

Author Comment

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

Expert Comment

by:nietod
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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 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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 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