您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C++如何實現掃描系統進程,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
把做工程過程比較重要的一些代碼片段備份一次,如下代碼是關于C++掃描系統進程的代碼。
#include <windows.h> #include <tlhelp32.h> #include <tchar.h> #include <stdio.h> #include<iostream> using namespace std; int main() { HANDLE hProcessSnap; HANDLE hProcess; if(hProcessSnap == INVALID_HANDLE_VALUE) { cout<<"快照創建失敗"<<endl; return 0; } pe32.dwSize = sizeof(PROCESSENTRY32); if(Process32First( hProcessSnap, &pe32) == NULL) { cout<<"error"<<endl; CloseHandle(hProcessSnap); } int i = 0; do{ cout<<"------------"<<i++<<"個進程------------------------"<<endl; _tprintf( TEXT("nPROCESS NAME: %s"), pe32.szExeFile ); cout<<endl; cout<< "Process ID = "<< pe32.th42ProcessID <<endl; cout<< "Thread count = "<<pe32.cntThreads <<endl; cout<< "Parent process ID = "<< pe32.th42ParentProcessID <<endl;; cout<< "Priority base = "<< pe32.pcPriClassBase <<endl; }while( Process32Next( hProcessSnap, &pe32) ); return 0; } 當然,上面程序是我從一個程序中摘出來的,這是源程序,忘了引用的哪了,如果作者看到,請告知引用地址。這個程序實現了對系統進程,單個進程和線程的快照,主要還是拿幾個函數,有時間的話做一個可視化的,和大家分享 #include <windows.h> #include <tlhelp32.h> #include <tchar.h> #include <stdio.h> BOOL GetProcessList( ); BOOL ListProcessModules( DWORD dwPID ); BOOL ListProcessThreads( DWORD dwOwnerPID ); void main( ) { GetProcessList( ); } BOOL GetProcessList( ) { HANDLE hProcessSnap; HANDLE hProcess; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); CreateToolhelp32Snapshot函數為指定的進程、進程使用的堆[HEAP]、模塊[MODULE]、線程[THREAD])建立一個快照[snapshot]。 if( hProcessSnap == INVALID_HANDLE_VALUE ) { printError( TEXT("CreateToolhelp32Snapshot (of processes)") ); return( FALSE ); if( !Process32First( hProcessSnap, &pe32 ) ) { ( FALSE ); do { printf( "nn=====================================================" ); _tprintf( TEXT("nPROCESS NAME: %s"), pe32.szExeFile ); dwPriorityClass = 0; hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th42ProcessID ); OpenProcess 函數用來打開一個已存在的進程對象,并返回進程的句柄。 if( hProcess == NULL ) printError( TEXT("OpenProcess") ); else { dwPriorityClass = GetPriorityClass( hProcess ); if( !dwPriorityClass ) printError( TEXT("GetPriorityClass") ); CloseHandle( hProcess ); } printf( "n Process ID = 0x%08X", pe32.th42ProcessID ); printf( "n Thread count = %d", pe32.cntThreads ); printf( "n Parent process ID = 0x%08X", pe32.th42ParentProcessID ); printf( "n Priority base = %d", pe32.pcPriClassBase ); if( dwPriorityClass ) } while( Process32Next( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap ); return( TRUE ); } BOOL ListProcessModules( DWORD dwPID ) { HANDLE hModuleSnap = INVALID_HANDLE_VALUE; hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID ); if( hModuleSnap == INVALID_HANDLE_VALUE ) { printError( TEXT("CreateToolhelp32Snapshot (of modules)") ); return( FALSE ); if( !Module32First( hModuleSnap, &me32 ) ) { return( FALSE ); do { _tprintf( TEXT("nn MODULE NAME: %s"), me32.szModule ); _tprintf( TEXT("n Executable = %s"), me32.szExePath ); printf( "n Process ID = 0x%08X", me32.th42ProcessID ); printf( "n Ref count (g) = 0x%04X", me32.GlblcntUsage ); printf( "n Ref count (p) = 0x%04X", me32.ProccntUsage ); printf( "n Base address = 0x%08X", (DWORD) me32.modBaseAddr ); printf( "n Base size = %d", me32.modBaseSize ); } while( Module32Next( hModuleSnap, &me32 ) ); CloseHandle( hModuleSnap ); return( TRUE ); } BOOL ListProcessThreads( DWORD dwOwnerPID ) { HANDLE hThreadSnap = INVALID_HANDLE_VALUE; hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 ); if( hThreadSnap == INVALID_HANDLE_VALUE ) if( !Thread32First( hThreadSnap, &te32 ) ) { return( FALSE ); do { if( te32.th42OwnerProcessID == dwOwnerPID ) { printf( "nn THREAD ID = 0x%08X", te32.th42ThreadID ); printf( "n Base priority = %d", te32.tpBasePri ); printf( "n Delta priority = %d", te32.tpDeltaPri ); } } while( Thread32Next(hThreadSnap, &te32 ) ); CloseHandle( hThreadSnap ); return( TRUE ); } { DWORD eNum; TCHAR sysMsg[256]; eNum = GetLastError( ); p = sysMsg; ++p; do { _tprintf( TEXT("n WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg ); }
關于“C++如何實現掃描系統進程”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。