1. 程式人生 > >VC獲取系統程序列表、查詢程序、關閉程序

VC獲取系統程序列表、查詢程序、關閉程序

1、獲取系統程序列表

  1. #include <tlhelp32.h>//宣告快照函式標頭檔案
  2. ...  
  3. PROCESSENTRY32 pe32;  
  4. pe32.dwSize=sizeof(pe32);  
  5. HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  
  6. if(hProcessSnap==INVALID_HANDLE_VALUE)  
  7. {  
  8.    MessageBox(L"CreateToolhelp32Snapshot呼叫失敗!\n");  
  9.    return -1;  
  10. }  
  11. //遍歷程序快照。輪流顯示每個程序的資訊
  12. CString strPrcNameID;  
  13. BOOL bMore=::Process32First(hProcessSnap,&pe32);  
  14. while(bMore)  
  15. {  
  16.    strPrcNameID.Format(L"程序名稱:%s,ID:%u\n",pe32.szExeFile,pe32.th32ProcessID);  
  17.    MessageBox(strPrcNameID);  
  18.    bMore=::Process32Next(hProcessSnap,&pe32);  
  19. }  
  20. //清除snapshot物件
  21. ::CloseHandle(hProcessSnap);  


2、查詢指定程序和關閉該程序

以下三個函式使用前先#include "Psapi.h",然後SETTING->LINK 裡新增Psapi.lib即可

  1. //查詢指定程序
  2. DWORD FindProcess(TCHAR *strProcessName)  
  3. {  
  4.     DWORD aProcesses[1024], cbNeeded, cbMNeeded;  
  5.     HMODULE hMods[1024];  
  6.     HANDLE hProcess;  
  7.     TCHAR szProcessName[MAX_PATH];  
  8.     if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )  
    return 0;  
  9.     for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)  
  10.     {  
  11.         hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);  
  12.         EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);  
  13.         GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));  
  14.         CString strPrcFullName(szProcessName);  
  15.         CString strPrcName(strProcessName);  
  16.         if(_tcsstr(strPrcFullName, strPrcName) || _tcsstr(strPrcFullName, strPrcName.MakeLower()))  
  17.         {  
  18.             CString strNameFull;  
  19.             strNameFull.Format(_T("Process full name:\n%s;"), szProcessName);  
  20.             //AfxMessageBox(strNameFull);
  21.             return(aProcesses[i]);  
  22.         }  
  23.     }  
  24.     return 0;  
  25. }  
  26. // 此函式利用上面的 FindProcess 函式獲得你的目標程序的ID
  27. // 用WIN API OpenPorcess 獲得此程序的控制代碼,再以TerminateProcess強制結束這個程序
  28. VOID KillProcess(TCHAR *strProcessName)  
  29. {  
  30.     // When the all operation fail this function terminate the "winlogon" Process for force exit the system.
  31.     HANDLE hYourTargetProcess = OpenProcess(PROCESS_QUERY_INFORMATION |   // Required by Alpha
  32.          PROCESS_CREATE_THREAD     |   // For CreateRemoteThread
  33.          PROCESS_VM_OPERATION      |   // For VirtualAllocEx/VirtualFreeEx
  34.          PROCESS_VM_WRITE          |  // For WriteProcessMemory
  35.          PROCESS_TERMINATE,           //Required to terminate a process using TerminateProcess function
  36.          FALSE, FindProcess(strProcessName));  
  37.     if(hYourTargetProcess == NULL)  
  38.     {  
  39.        DWORD ulErrCode = GetLastError();  
  40.        CString strError;  
  41.        strError.Format(L"OpenProcess failed,error code:%ld", ulErrCode);  
  42.        AfxMessageBox(strError);  
  43.     }  
  44.     BOOL result = TerminateProcess(hYourTargetProcess, 0);  
  45.     if(!result)  
  46.     {  
  47.         DWORD ulErrCode = GetLastError();  
  48.         CString strError;  
  49.         strError.Format(L"TerminateProcess failed,error code:%ld", ulErrCode);  
  50.         AfxMessageBox(strError);  
  51.     }  
  52.     return;  
  53. }  
  54. // 在 Windows NT/2000/XP 中可能因許可權不夠導致以上函式失敗
  55. // 如以 System 許可權執行的系統程序,服務程序用本函式取得 debug 許可權即可,Winlogon.exe 都可以終止哦 :)
  56. BOOL GetDebugPriv()  
  57. {  
  58.     HANDLE hToken;  
  59.     LUID sedebugnameValue;  
  60.     TOKEN_PRIVILEGES tkp;  
  61.     if ( ! OpenProcessToken( GetCurrentProcess(),  
  62.     TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )  
  63.     {  
  64.         return FALSE;  
  65.     }  
  66.     if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) )  
  67.     {  
  68.         CloseHandle( hToken );  
  69.         return FALSE;  
  70.     }  
  71.     tkp.PrivilegeCount = 1;  
  72.     tkp.Privileges[0].Luid = sedebugnameValue;  
  73.     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
  74.     if (!AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) )  
  75.     {  
  76.         CloseHandle( hToken );  
  77.         return FALSE;  
  78.     }  
  79.     return TRUE;  
  80. }  
轉載出處:http://hi.baidu.com/wllbydtqahbegqq/item/9506b15e4429159c08be177e

                    http://blog.csdn.net/agan2007/article/details/1776812