C++ and STL
  Common Issues
  Destructor
  regex
  string
 MFC
  CButton
  CDialog
  CEdit
  CInternetSession
  CWinThread
  CWnd
 MS VS FAQ
  Compiling, building
  Debugging
  Editor
  Settings
 Win32, API
  Console
  File System
  Graphics
  Internet functions
  Kernel objects
  Security
  Sound
  Thread, process
  Window
 Windows NT/2K*
  Logon, logoff...
  Networking
  Service Packs
 | About
  About
  Links & Freeware

Powered by
CoderTown



Usage of argv[0]

int main(int argc, char* argv[])
{
   return 0;
}

Everybody recognizes this line. In some sources you can find that the argv[0] variable contains a full(?) path to the executable started including its name.

Recently I had noticed it is not always true. My statement is the argv[0] contains a string which was provided to the system to start the file (without following command line arguments), whatever it was, namely it can be a file name without an extenstion and path, a name and extension with no path, or a full path plus file name etc... Actually, argv[0] is a command line argument number 0, which can be anything sufficient to start an application and not necessary the full path to the file.

To prove my words you may do the following: compile this snippet.

#include "stdafx.h" //or #include "stdio.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{
   printf("_pgmptr = %s\n", _pgmptr);
   printf("argv[0] = %s\n", argv[0]);
   printf("Hello World!\n");
   return 0;
}

Place it somewhere on you hard drive. Start cmd. Set current directory to the folder where the file is. Run it by name with extension. Run it by name without extention. Run it by entering full path to the file. Check the output. argv[0] is always different.

See that run-time global variable _pgmptr? For me it was always the same and equal to complete full path including name and extension... So, be aware what is going on under the hood even in such a simple thing.

Created: 2003-09-05
Updated: 2003-09-05

Google
 
Web visualcpp.net
msdn.microsoft.com codeguru.com