Microsoft KB Archive/47037

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

INFO: Using _pgmptr to Get the Full Path of Executing Program

Article ID: Q47037

The information in this article applies to:

  • The C Run-time (CRT) included with: - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, 6.0ax - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0

SUMMARY

In Microsoft C, the variable _pgmptr is not defined in an include file. It is declared in CRT0DAT.ASM, which is part of the C startup code. This code is linked to any module that contains a main() function. To use _pgmptr, it must first be declared as an external far character pointer, as follows:

   extern char far *_pgmptr;

Because _pgmptr is automatically initialized at startup to point to the full path of the executing program, only this declaration is required to make the full path available to your program. However, in Visual C++, _pgmptr is defined in stdlib.h, and the above declaration is no longer necessary.

MORE INFORMATION

The sample code below demonstrates how to use _pgmptr to obtain the full path and file name of the executing program for a console application. _pgmptr may also be used in Windows applications, both Win16 and Win32. For more information on _pgmptr as it applies to Windows applications, search the Visual C++ Books Online for "_pgmptr".

Sample Code

   /* Compile options needed: none.
   */ 

   #include <stdio.h>

   /* Note how _pgmptr is declared conditionally. For any version of
      Visual C++, _MSC_VER >= 800.
   */ 

   #if _MSC_VER <= 800
   extern char far *_pgmptr;
   #else
   #include <stdlib.h>
   #endif

   void main(void)
   {
      printf ("The full path of the executing program is : %Fs\n",
              _pgmptr);
   }

In OS/2 real mode or MS-DOS 3.x, argv[0] also contains a pointer to the full path of the executing program. In OS/2 protected mode or Windows NT, argv[0] contains only the information typed at the command line to invoke the program. In these environments, using _pgmptr is the only way to easily access the executing program's full path string.

Keywords          : kbcode kbCRT kbVC 
Version           : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,4.0,5.0
Platform          : MS-DOS NT WINDOWS
Issue type        : kbinfo

Last Reviewed: May 23, 1998
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.