PROCESSENTRY32结构之中的szExeFile可以在98之中显示进程的全路径,但是在XP之下就不能了。请问诸位高手,如何获得进程的全路径?谢谢!

解决方案 »

  1.   

    char szPath[_MAX_PATH];
    GetModuleFileName(GetModuleHandle(NULL), szPath, _MAX_PATH);
      

  2.   

    void GetProcessName(DWORD processid,LPTSTR buf,int len)
    {
    //make sure buf is valid and long enough
    if(buf==NULL||len<=0)
    return;
    buf[0]=0;
    if(processid ==8)
    {
    _tcscpy(buf,"System");
    return;
    }
    if(processid==0)
    {
    _tcscpy(buf,"System Idle Process");
    return;
    }
    HANDLE hProcess =OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,false,processid);
    if(hProcess ==NULL)
    {
    _tcscpy(buf,"unknown(OpenProcess error)");
    return;
    }
    HMODULE hModule;
    DWORD cbReturned;
    BOOL bret=EnumProcessModules(hProcess ,&hModule, sizeof(hModule), &cbReturned );
    if(bret)
    {
    DWORD dwret=GetModuleFileNameEx(hProcess ,hModule,buf,len);
    //GetModuleBaseName(hProcess,hModule,buf,len);
    }
    else
    _tcscpy(buf,"unknown(GetModuleBaseName error)");
    CloseHandle( hProcess  ) ;
    // GetProcessName2(processid,buf);
    }
      

  3.   

    Masterz的程序的代码在98下编译不过,即使在2k下编译过了在2K下执行也是出错.我试过.
      

  4.   

    我最近作了各类似的: (我是获得若干进程的全路径,主要用到CreateToolhelp32Snapshot和GetModuleFileNameEx,path就是全路径,你自己把不要的去掉就可以了)
    HANDLE  hSnapShot = NULL, hExeFile = NULL; 
    BOOL bRet = FALSE ;
    CString show ; hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapShot == INVALID_HANDLE_VALUE) 
            return bRet;  PROCESSENTRY32 info ;
        info.dwSize = sizeof(info); if(Process32First(hSnapShot, &info))
    {
    if(GetLastError() == ERROR_NO_MORE_FILES )
    {
    AfxMessageBox("没有进程信息 !") ;
    CloseHandle(hSnapShot);
    return bRet ;
    }
    else
    {
    CString ExeFile, ExePath;
    char path[255] ;
    memset(path, 0, sizeof(path)) ;
     
    int nCount = arry.GetSize() ;
    int nIndex = 0 ;
    int pos = 0 ;
    for (int i = 0; i < nCount; i++)
    {
    nIndex = arry.GetAt(i) ;
    ExeFile = m_csArry.GetAt(nIndex)->name ;
    ExePath = ExeFile ;
    pos = ExeFile.ReverseFind('\\') ;
    pos = ExeFile.GetLength() - pos - 1 ;
    ExeFile = ExeFile.Right(pos)  ; 

    while (Process32Next(hSnapShot, &info))
    {
    if (ExeFile == info.szExeFile)
    {
    hExeFile = OpenProcess(PROCESS_ALL_ACCESS, FALSE, info.th32ProcessID);
    if(hExeFile != NULL)
    {
    GetModuleFileNameEx(hExeFile, (HMODULE)(info.th32ModuleID), path, 255) ;
    if (ExePath == path)
    {
    TerminateProcess(hExeFile, 0);
    m_list2.SetItemText(nIndex, 1, "未运行") ;
    m_list2.SetItemText(nIndex, 2, "") ;
    m_csArry.GetAt(nIndex)->flag = false ;
    break ;
    }
    }
    }
    }
    }
    bRet = TRUE ;
    }
    }
    else
    {
    AfxMessageBox(" 系统错误 !") ;
    }
        CloseHandle(hSnapShot);
    return bRet ;
      

  5.   

    GetModuleFileName 不行么
      

  6.   

    Toyh824(啾啾虫):
    为什么在MSDN中可以找到GetModuleFileNameEx,而我的VC 6.0不能使用?
      

  7.   

    要包含头文件才可以用GetModuleFileNameEx
      

  8.   

    To Oversense(步步文):
    哪个头文件?我用Find in files...找过了,没找到哦。
    谢谢!
      

  9.   

    Requirements 
      Windows NT/2000: Requires Windows NT 4.0 or later.
      Header: Declared in Psapi.h.
      Library: Use Psapi.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
      

  10.   

    哦,是这样,#include "psapi" 就OK了
      

  11.   

    谢谢诸位热心的解答,我的VC 6.0并没带有psapi.h和psapi.lib,我从VS.net 2003中复制了一份,运行正常。但是仍然无法获得那些系统服务(如svchost.exe等)的全路径,请问如何解决?