如何在我自己的程序中通过函数判断一个其他的应用程序是否运行,
比如说有一个应用程序XXXX.exe在任务管理器->进程:映象名称中的名字也是
XXXX.exe,我怎么判断它是否已经运行?

解决方案 »

  1.   

    1. Get a snapshot of the current system using either
    CreateToolhelp32Snapshot , Process32First and Process32Next  incase of 95,
    or EnumProcesses in case of NT and getting the exe file name from each of
    them and checking for the relevant name.
    2. If you know the Window class name and the title of the window created,
    using the FindWindow () API.
      

  2.   

    用CreateToolhelp32Snapshot 如何取得get the exe file name ?
    能详细介绍一下吗,我用的系统是win2000。
      

  3.   

    http://www.codeguru.com/system/PList.html
    single interface to enumerate processes//enum process to find a certain module
    void CPtbView::IsAppRun(CString modulename)
    {
    DWORD buf[4096];
    DWORD num;
    TCHAR filenamebuf[_MAX_PATH+1];
    HMODULE hModule;
    DWORD cbReturned;
    BOOL bret=EnumProcesses(buf,4095,&num);
    bool bfound=false;
    CString msg;

    if(!bret)
    {
    AfxMessageBox("Error EnumProcesses");
    return;
    }

    for(int i=0;i<(int)num;i++)
    {
    HANDLE hProcess =OpenProcess(PROCESS_QUERY_INFORMATION&brvbar; PROCESS_VM_READ,false,buf[i]);
    if(hProcess ==NULL)
    continue;
    bret=EnumProcessModules(hProcess ,&hModule, sizeof(hModule), &cbReturned );
    if(bret)
    {
    DWORD dwret=GetModuleFileNameEx(hProcess ,hModule,filenamebuf,_MAX_PATH);
    CloseHandle( hProcess  ) ;
    if(dwret==0)
    {
    msg.Format("%d",GetLastError());
    AfxMessageBox(msg);
    break;
    }
    else
    {
    TCHAR* pfind=_tcsstr(filenamebuf,modulename);
    if(pfind)
    {
    bfound=true;
    break;
    }
    }
    }
    }
    if(bfound)
    AfxMessageBox("Found it");
    else
    AfxMessageBox("Not found");
    }void CPtbView::OnViewCheckrun()
    {
    IsAppRun("notepad.exe");
    }
      

  4.   

    很感谢楼上的大侠,可是缺少什么头文件好像吧,出现
    error C2065: 'EnumProcesses' : undeclared identifier
    error C2065: 'EnumProcessModules' : undeclared identifier
    error C2065: 'GetModuleFileNameEx' : undeclared identifier
    编译错误。还请指教
      

  5.   

    fatal error C1083: Cannot open include file: 'psapi.h': 
    No such file or directory
     我加了,但是出现这样的错误
      

  6.   

    Windows NT/2000: Requires Windows NT 4.0 or later.
    you can't use it in win98!
      

  7.   

    大侠,在2000下出现
    fatal error C1083: Cannot open include file: 'psapi.h': 
    No such file or directory
      

  8.   

    楼上,我用的是Windows 2000,也不行,也就是没有Psapi.h这个文件,我在网上浏览了许多地方,PSAPI下了一个又不能用,
      

  9.   

    do you add #include "Psapi.h" and link Psapi.lib?
    i build it successfully just now.
      

  10.   

    在msdn光盘下找到psapi.h与psapi.lib这两个文件分别拷到VC安装目录下的include目录与lib目录即可.
    我试过了.
      

  11.   

    可以用了,要将psapi.lib加入setting中, kingzai(kingzai) ( )高手中有一句copy错了
    HANDLE hProcess =OpenProcess(PROCESS_QUERY_INFORMATION&brvbar; PROCESS_VM_READ,false,buf[i]);改为
    HANDLE hProcess =OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,false,buf[i]);
      

  12.   

    "Psapi.h" 好像只有VC.net才有
      

  13.   

    在Win9x/Win2000/WinXP下取得进程的带路径的文件名有通用的方法吗?
      

  14.   

    我把psapi.h与psapi.lib分别拷到include目录与lib目录里怎么还是报一样的错啊?error C2065: 'EnumProcesses' : undeclared identifier
    error C2065: 'EnumProcessModules' : undeclared identifier
    error C2065: 'GetModuleFileNameEx' : undeclared identifier