wince6 應用程序殺掉其他進程

//殺掉進程函數
HRESULT KillProcessByName(LPCTSTR lpProcessName)
{
    HRESULT hr = S_OK;
    HANDLE hnd;
    PROCESSENTRY32 pe32;
    if (NULL == lpProcessName)
    {
        return E_INVALIDARG;
    }
    //Takes a snapshot of the processes
    hnd = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
    if(hnd == INVALID_HANDLE_VALUE)
    {
        DEBUGMSG(TRUE, TEXT("KillProcessByName: INVALID_HANDLE_VALUE"));
        return E_FAIL;
    }
    pe32.dwSize = sizeof(pe32);
    //enumerate the processes
    BOOL bMore = Process32First(hnd,(LPPROCESSENTRY32)&pe32);
    while(bMore)
    {
        _TCHAR aCompareProcessName[MAX_STR];
        _tcscpy_s(aCompareProcessName,MAX_STR,pe32.szExeFile);
        _tcslwr_s(aCompareProcessName,MAX_STR);
        //Do: compare the process with the name
        if(lstrcmp(lpProcessName,aCompareProcessName)==0)
        {
            DEBUGMSG(TRUE, (TEXT("The Process:%s with ID: %X will be killed"),
                pe32.szExeFile,
                pe32.th32ProcessID));
            //Get the handler
            HANDLE h_ProcessHandle = OpenProcess(
                PROCESS_ALL_ACCESS,
                FALSE,
                pe32.th32ProcessID);
            if(NULL == h_ProcessHandle)
            {
                DEBUGMSG(TRUE, TEXT("ERROR: The Process should already exit"));
                CloseToolhelp32Snapshot(hnd);
                hr = S_OK;
                break;
            }
            DWORD exitCode =0;
            //Terminate the process
            if(!TerminateProcess(h_ProcessHandle,exitCode))
            {
                hr = E_FAIL;
                break;
            }
        }//end of do
        bMore = ::Process32Next(hnd,(LPPROCESSENTRY32)&pe32);
    }// End of while
    //closes a handle to a snapshot.
    CloseToolhelp32Snapshot(hnd);
    return hr;

}



調用

KillProcessByName(L"123.exe");//先殺掉進程再啓動




發佈了34 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章