这篇文章将为大家详细讲解有关C++如何实现扫描系统进程,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联公司是专业的特克斯网站建设公司,特克斯接单;提供网站制作、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行特克斯网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
把做工程过程比较重要的一些代码片段备份一次,如下代码是关于C++扫描系统进程的代码。
#include#include #include #include #include using namespace std; int main() { HANDLE hProcessSnap; HANDLE hProcess; if(hProcessSnap == INVALID_HANDLE_VALUE) { cout<<"快照创建失败"< #include #include #include 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++如何实现扫描系统进程”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。