Solved

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

Posted on 2001-07-08
10
4,556 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now