这篇文章将为大家详细讲解有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。