home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / ProgramPath.c < prev    next >
Text File  |  2000-08-26  |  1KB  |  80 lines

  1. #pragma strings(readonly)
  2.  
  3. #define INCL_DOSPROCESS
  4. #define INCL_DOSMODULEMGR
  5. #define INCL_DOSERRORS
  6.  
  7. #include <os2.h>
  8.  
  9. #include <stdio.h>
  10.  
  11.  
  12. BOOL _Inline get_process_modulename(ULONG cbName, PCH pch);
  13.  
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.    char achTmp[CCHMAXPATHCOMP] = "";
  18.  
  19.    printf("argv[0] = %s\n", argv[0]);
  20.  
  21.    if(get_process_modulename(sizeof(achTmp), achTmp))
  22.    {
  23.       char *p = achTmp;
  24.  
  25.       printf("module pathname: %s\n", achTmp);
  26.  
  27.       /*
  28.        * Quick and dirty removal of the filename
  29.        */
  30.       while(*p)
  31.       {
  32.          p++;
  33.       }
  34.  
  35.       while(*p != '\\')
  36.       {
  37.          p--;
  38.       }
  39.  
  40.       p+=1;
  41.       *p = '\0';
  42.  
  43.       printf("program path: %s\n", achTmp);
  44.  
  45.    }
  46.    else
  47.    {
  48.       puts("Error: Couldn't get module information");
  49.    }
  50.  
  51.  
  52.    return 0;
  53. }
  54.  
  55. BOOL _Inline get_process_modulename(ULONG cbName, PCH pch)
  56. {
  57.    BOOL fSuccess = FALSE;
  58.    APIRET rc = NO_ERROR;
  59.    PTIB ptib = NULL;
  60.    PPIB ppib = NULL;
  61.  
  62.    /*
  63.     * Get the pointers to the process and thread info blocks.
  64.     */
  65.    rc = DosGetInfoBlocks(&ptib, &ppib);
  66.    if(rc == NO_ERROR)
  67.    {
  68.       /*
  69.        * The process info block contains the module handle,
  70.        * use it to get the path of the module.
  71.        */
  72.       rc = DosQueryModuleName(ppib->pib_hmte, cbName, pch);
  73.       if(rc == NO_ERROR)
  74.       {
  75.          fSuccess = TRUE;
  76.       }
  77.    }
  78.    return fSuccess;
  79. }
  80.