比如要启动a.exe的时候判断b.exe有没有在运行,如果b.exe已经启动则a.exe就不能启动,没有则可以启动。

解决方案 »

  1.   


    void CloseProgram(CString strProgram)
    {
    HANDLE handle; //定义CreateToolhelp32Snapshot系统快照句柄 
    HANDLE handle1; //定义要结束进程句柄 
    handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获得系统快照句柄 
    PROCESSENTRY32 *info; //定义PROCESSENTRY32结构字指 
    //PROCESSENTRY32 结构的 dwSize 成员设置成 sizeof(PROCESSENTRY32)  info = new PROCESSENTRY32; 
    info->dwSize = sizeof(PROCESSENTRY32); 
    //调用一次 Process32First 函数,从快照中获取进程列表 
    Process32First(handle, info); 
    //重复调用 Process32Next,直到函数返回 FALSE 为止 
    while(Process32Next(handle, info) != FALSE) 
    {   
    CString strTmp = info->szExeFile;     //指向进程名字   
    //strcmp字符串比较函数同要结束相同   
    //if(strcmp(c, info->szExeFile) == 0 )   
    if (strProgram.CompareNoCase(info->szExeFile) == 0 )   
    {   
    //PROCESS_TERMINATE表示为结束操作打开,FALSE=可继承,info->th32ProcessID=进程ID    
    handle1 = OpenProcess(PROCESS_TERMINATE, FALSE, info->th32ProcessID); 
    //结束进程    
    TerminateProcess(handle1, 0);    
    }   
    }
    delete info; CloseHandle(handle);
    }
      

  2.   

    查找b.exe进程,也就是枚举进程。。根据这个来判断。。
      

  3.   

    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include "psapi.h"void PrintProcessNameAndID( DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");    // Get a handle to the process.    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );    // Get the process name.    if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }    // Print the process name and identifier.    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );    CloseHandle( hProcess );
    }void main( )
    {
        // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name and process identifier for each process.    for ( i = 0; i < cProcesses; i++ )
            PrintProcessNameAndID( aProcesses[i] );
    }
      

  4.   

    如果b是别人的程序,就用2L的方法,枚举进程。
    如果a、b都是你的程序,或者说是单实例程序,就生成一个GUID创建Mutex就是了,只要系统中有进程创建过了,CreateMutex后GetLastError的值就是ERROR_ALREADY_EXISTS
      

  5.   

    bool CTestDlgDlg::IsOpen(TCHAR* strProcess)
    {
    PROCESSENTRY32 pe32;
    pe32.dwSize=sizeof(pe32);
    HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if(hProcessSnap==INVALID_HANDLE_VALUE)
    {
    printf("CreatToolhelp32Snapeshot调用失败!\n");
    return -1;
    }
    BOOL bMore=Process32First(hProcessSnap,&pe32);//获得第一个进程的句柄。注意是第一个所以还要while()
    while(bMore)
    {
    bMore=Process32Next(hProcessSnap,&pe32);//这里要注意,必须循环,因为你只运行一次函数,只能取到进程
    CString strFindFile;
    strFindFile.Format(_T("%s"), strProcess);
    CString strExeFile;
    strExeFile.Format(_T("%s"), pe32.szExeFile);
    if (strExeFile.CompareNoCase(strFindFile) == 0)
    {
    CloseHandle(hProcessSnap);
    return true;
    }
    }
    CloseHandle(hProcessSnap);
    return false;
    }
      

  6.   

    枚举进程,EnumProcesses()找到b进程
      

  7.   

    先包含#include <tlhelp32.h>HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32* info=new PROCESSENTRY32;
    info->dwSize=sizeof(PROCESSENTRY32);
    int i=0;
            
    if(Process32First(handle,info))
    {
      if(GetLastError()==ERROR_NO_MORE_FILES )
     {
    AfxMessageBox("No More Process");
     }
    else
    {
            CString ProName;
    ProName.Format("%s",info->szExeFile);
    if(ProName == "b.exe")
    { 你要干的事!  }
     }
    }