EXENAME - How to query fully qualified name of current executable from program source

From EDM2
Jump to: navigation, search

By Joseph Nord

Example OS/2 PDD code demonstrating how to query the fully path qualified name of the executable program.

License

You can use this name of executable demonstrating code for what ever you want including commercial products with no restriction on use or royalty. If you find it useful, I would appreciate it if you give me a plug in the source and drop me a note to let me know where you are using the code.

Example source code (exename.c)

/*
** This program demonstrates how an OS/2 .exe file
** can determine the path from which it was loaded.
**
** This eliminates dependance on DPATH and should
** speed access to program dependant files.
**
** It is designed for execution as a 32 Bit OS/2 application.
**
** Joe Nord 29-Jan-92
*/
#include <stdlib.h>
#include <stdio.h>

#define INCL_DOSPROCESS
#include <os2.h>

int main (int argc, char *argv[])
{
  PSZ   pszEnv;
  ULONG ulRC;

  PTIB ptib;
  PPIB ppib;

  ulRC = DosGetInfoBlocks (&ptib, &ppib);
  if (ulRC)
    {
    printf ("Error: DosGetInfoBlocks call failed, RC = %li.\n", ulRC);
    return (1);
    }

  pszEnv = ppib->pib_pchenv;

  /*
  ** Seach environment for first occurance of
  ** two successive zero bytes.  This signals
  ** the end of the environment structure.
  */
  while (*pszEnv != '\0')
  {
    while (*(++pszEnv) != '\0')  /* Find a single zero byte */
      ; /* Null statement */
    pszEnv++;                    /* Point to first char past first zero byte */
  } /* falls out of loop on two successive zero bytes */

  /*
  ** The name of the executing program is stored (with full path)
  ** as an ASCII Z string immediately beyond the environment.
  */
  pszEnv++;
  printf ("%s\n", pszEnv);  /* Executable name */

  return (0);
}