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

Last reviewed: September 2, 1997
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          : CRTIss kbcode kbcode kbfasttip
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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.