c++ 怎么显示运行程序的完整路径???

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

我现在编写一个对系统正在运行进程进行管理的程序,类似任务管理器,  
  在网上找到一个代码能够得到运行的进程名和进程的路径,但路径不是完整的,  
  哪位能帮我想办法得到程序的完整路径?  
  程序源码如下:  
   
  /*  
    * showprocesspath_thapi  
    * 版权所有   (c)   2004   赵春生  
    * 2004.08.08  
    * http://timw.yeah.net  
    * http://timw.126.com  
    * 本程序适用于:winnt  
    * 代码在win2000p+sp4   +   vc6+sp5测试通过  
  */  
   
  #include   <stdio.h>  
  #include   <windows.h>  
  #include   <tlhelp32.h>  
   
  int   main(void)  
  {  
   
  handle   hprocess=createtoolhelp32snapshot(th32cs_snapprocess,0);  
  handle   hmodule;  
  processentry32*   pinfo=new   processentry32;  
  moduleentry32*   minfo=new   moduleentry32;  
  pinfo->dwsize=sizeof(processentry32);  
  minfo->dwsize=sizeof(moduleentry32);  
  bool   report;  
  char   shortpath[max_path]   =   "";  
   
  printf("showprocesspath   with   [toolhelp   api]\n\n");  
   
  report=process32first(hprocess,pinfo);  
   
  while(report)  
  {  
  hmodule=createtoolhelp32snapshot(th32cs_snapmodule,pinfo->th32processid);  
  module32first(hmodule,   minfo);  
   
  getshortpathname(minfo->szexepath,shortpath,256);  
  printf("%s   ---   %s\n",pinfo->szexefile,shortpath);  
  report=process32next(hprocess,   pinfo);    
   
  }  
   
   
  closehandle(hprocess);  
  closehandle(hmodule);  
   
  printf("\n");  
   
  return   0;  
  }  
   
   
  运行部分结果如下:  
  conime.exe   ---   c:\winnt\system32\conime.exe  
  popo.exe   ---   c:\progra~1\netease\popo2004\popo.exe  
  taskmgr.exe   ---   c:\winnt\system32\taskmgr.exe  
  msdev.exe   ---   c:\progra~1\micros~2\common\msdev98\bin\msdev.exe  
  maxthon.exe   ---   c:\progra~1\maxthon\maxthon.exe  
  vcspawn.exe   ---   c:\progra~1\micros~2\common\msdev98\bin\vcspawn.exe  
  showprocesspath   ---   f:\20050712\showpr~1\showpr~1\debug\showpr~1.exe  
   
   
  结果中popo.exe的路径是c:\progra~1\netease\popo2004\popo.exe  
  我想得到它的完整路径"c:\program   files\netease\popo2004\popo.exe"  
   
  哪位高手能帮我想想办法解决这个问题!

· 网友精彩回答:

发表者:kkk3k3k

char   drive[_max_drive];       char   dir[_max_dir];       char   fname[_max_fname];       char   ext[_max_ext];  
  char   szpath[max_path];  
  getmodulefilename(null,   szpath,   sizeof(szpath)   /   sizeof(szpath[0]));  
  _splitpath(szpath,   drive,   dir,   fname,   ext   );  
       
        gappdir+=drive;  
        gappdir+=dir;

发表者:goodboy1881

int   main(int   argc,char**   argv){  
            printf("path   is   %s\n",argv[0]);  
            system("pause");  
            return   0;  
  }  
 

发表者:seektruth

getshortpathname得出来的路径就是这样子,你可以直接使用moduleentry32的成员  
  szexepath.

发表者:william_wang

用api吧,getcurrentdirectory()

发表者:openhero

#include   <direct.h>  
  _getcwd()

发表者:mdj_boy

将:getshortpathname  
  改成:getlongpathname

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved