![]() |
遍历进程
//EnumProc.cpp
//////////////////////////////////////////////////////////////// // MSDN Magazine -- July 2002 // If this code works, it was written by Paul DiLascia. // If not, I don't know who wrote it. // Compiles with Visual Studio 6.0 and Visual Studio .NET // Runs in Windows XP and probably Windows 2000 too. // #include "stdafx.h" #include "EnumProc.h" CWindowIterator::CWindowIterator(DWORD nAlloc) { assert(nAlloc>0); m_current = m_count = 0; m_hwnds = new HWND [nAlloc]; m_nAlloc = nAlloc; } CWindowIterator::~CWindowIterator() { delete [] m_hwnds; } ////////////////// // Move to first top-level window. // Stuff entire array and return the first HWND. // HWND CWindowIterator::First() { ::EnumWindows(EnumProc, (LPARAM)this); m_current = 0; return Next(); } ////////////////// // Static enumerator passes to virtual fn. // BOOL CALLBACK CWindowIterator::EnumProc(HWND hwnd, LPARAM lp) { return ((CWindowIterator*)lp)->OnEnumProc(hwnd); } ////////////////// // Virtual enumerator proc: add HWND to array if OnWindow is TRUE. // BOOL CWindowIterator::OnEnumProc(HWND hwnd) { if (OnWindow(hwnd)) { if (m_count < m_nAlloc) m_hwnds[m_count++] = hwnd; } return TRUE; // keep looking } ////////////////// // Main window iterator: special case to iterate main windows of a process. // CMainWindowIterator::CMainWindowIterator(DWORD pid, DWORD nAlloc) : CWindowIterator(nAlloc) { m_pid = pid; } CMainWindowIterator::~CMainWindowIterator() { } ////////////////// // virtual override: is this window a main window of my process? // BOOL CMainWindowIterator::OnWindow(HWND hwnd) { if (GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE) { DWORD pidwin; GetWindowThreadProcessId(hwnd, &pidwin); if (pidwin==m_pid) return TRUE; } return FALSE; } CProcessIterator::CProcessIterator() { m_pids = NULL; } CProcessIterator::~CProcessIterator() { delete [] m_pids; } ////////////////// // Get first process. // Call EnumProcesses to initialize entire array, then return first one. // DWORD CProcessIterator::First() { m_current = (DWORD)-1; m_count = 0; DWORD nalloc = 1024; do { delete [] m_pids; m_pids = new DWORD [nalloc]; if (EnumProcesses(m_pids, nalloc*sizeof(DWORD), &m_count)) { m_count /= sizeof(DWORD); m_current = 1; // important: skip IDLE process with pid=0. } } while (nalloc <= m_count); return Next(); } CProcessModuleIterator::CProcessModuleIterator(DWORD pid) { // open the process m_hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); } CProcessModuleIterator::~CProcessModuleIterator() { CloseHandle(m_hProcess); delete [] m_hModules; } ////////////////// // Get first module in process. Call EnumProcesseModules to // initialize entire array, then return first module. // HMODULE CProcessModuleIterator::First() { m_count = 0; m_current = (DWORD)-1; m_hModules = NULL; if (m_hProcess) { DWORD nalloc = 1024; do { delete [] m_hModules; m_hModules = new HMODULE [nalloc]; if (EnumProcessModules(m_hProcess, m_hModules, nalloc*sizeof(DWORD), &m_count)) { m_count /= sizeof(HMODULE); m_current = 0; } } while (nalloc <= m_count); } return Next(); } |
回复: 遍历进程
//2)EnumProc.h
//////////////////////////////////////////////////////////////// // MSDN Magazine -- July 2002 // If this code works, it was written by Paul DiLascia. // If not, I don't know who wrote it. // Compiles with Visual Studio 6.0 and Visual Studio .NET // Runs in Windows XP and probably Windows 2000 too. // #pragma once ////////////////// // Iterate the top-level windows. Encapsulates ::EnumWindows. // class CWindowIterator { protected: HWND* m_hwnds; // array of hwnds for this PID DWORD m_nAlloc; // size of array DWORD m_count; // number of HWNDs found DWORD m_current; // current HWND static BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lp); // virtual enumerator virtual BOOL OnEnumProc(HWND hwnd); // override to filter different kinds of windows virtual BOOL OnWindow(HWND hwnd) { return TRUE; } public: CWindowIterator(DWORD nAlloc=1024); ~CWindowIterator(); DWORD GetCount() { return m_count; } HWND First(); HWND Next() { return m_hwnds && m_current < m_count ? m_hwnds[m_current++] : NULL; } }; ////////////////// // Iterate the top-level windows in a process. // class CMainWindowIterator : public CWindowIterator { protected: DWORD m_pid; // process id virtual BOOL OnWindow(HWND hwnd); public: CMainWindowIterator(DWORD pid, DWORD nAlloc=1024); ~CMainWindowIterator(); }; ////////////////// // Process iterator -- iterator over all system processes // Always skips the first (IDLE) process with PID=0. // class CProcessIterator { protected: DWORD* m_pids; // array of procssor IDs DWORD m_count; // size of array DWORD m_current; // next array item public: CProcessIterator(); ~CProcessIterator(); DWORD GetCount() { return m_count; } DWORD First(); DWORD Next() { return m_pids && m_current < m_count ? m_pids[m_current++] : 0; } }; ////////////////// // Iterate the modules in a process. Note that the first module is the // main EXE that started the process. // class CProcessModuleIterator { protected: HANDLE m_hProcess; // process handle HMODULE* m_hModules; // array of module handles DWORD m_count; // size of array DWORD m_current; // next module handle public: CProcessModuleIterator(DWORD pid); ~CProcessModuleIterator(); HANDLE GetProcessHandle() { return m_hProcess; } DWORD GetCount() { return m_count; } HMODULE First(); HMODULE Next() { return m_hProcess && m_current < m_count ? m_hModules[m_current++] : 0; } }; |
回复: 遍历进程
//3)lp.cpp
//////////////////////////////////////////////////////////////// // MSDN Magazine -- July 2002 // If this code works, it was written by Paul DiLascia. // If not, I don't know who wrote it. // Compiles with Visual Studio 6.0 and Visual Studio .NET // Runs in Windows XP and probably Windows 2000 too. // // lp -- list processes from command line. // Illustrates how to use the various iterator classes in EnunmProc.h/cpp // #include "stdafx.h" #include "EnumProc.h" #define tpf _tprintf // to hide ugly underbars // pre-declare functions void help(); // global switches BOOL bClassName=FALSE; // show window classname BOOL bTitle=FALSE; // show window title BOOL bBare=FALSE; // no header // check for switch: / or - inline BOOL isswitch(TCHAR c) { return c==L'/' || c==L'-'; } ////////////////// // Main entry point // int main(int argc, TCHAR* argv[], TCHAR* envp[]) { // Parse command line. Switches can come in any order. // for (int i=1; i<argc; i++) { if (isswitch(argv[i][0])) { for (UINT j=1; j<_tcslen(argv[i]); j++) { switch(tolower(argv[i][j])) { case 'c': bClassName=TRUE; break; case 't': bTitle=TRUE; break; case 'b': bBare=TRUE; break; case '?': default: help(); return 0; } } } else { help(); return 0; } } // Iterate over all processes // int count=0; BOOL bFirstModule = TRUE; CProcessIterator itp; for (DWORD pid=itp.First(); pid; pid=itp.Next()) { // Note: first module in CProcessModuleIterator is EXE for this process // TCHAR modname[_MAX_PATH]; CProcessModuleIterator itm(pid); HMODULE hModule = itm.First(); // .EXE if (hModule) { GetModuleBaseName(itm.GetProcessHandle(), hModule, modname, _MAX_PATH); // Iterate over all top-level windows in process // BOOL bFirstWindow = TRUE; CMainWindowIterator itw(pid); for (HWND hwnd = itw.First(); hwnd; hwnd=itw.Next()) { if (bFirstModule) { if (!bBare) { tpf(_T("PID %-13s%-8s %s%s%s\n"),_T("Module"),_T("HWND"), bClassName ? _T("class name") : _T(""), bClassName && bTitle ? _T("/") : _T(""), bTitle ? _T("title") : _T("")); tpf(_T("---- ------------ -------- ----------------\n")); } bFirstModule = FALSE; } if (bFirstWindow) { tpf(_T("%04x %-13s"), pid, modname); bFirstWindow = FALSE; } else { tpf(_T("%18s"),_T(" ")); } char classname[256],title[256]; GetClassName(hwnd,classname,sizeof(classname)); GetWindowText(hwnd,title,sizeof(title)); tpf(_T("%08x %s %s\n"), hwnd, bClassName ? classname : _T(""), bTitle ? title : _T("")); } bFirstWindow || count++; } } if (!bBare) { tpf(_T("----\n")); tpf(_T("%d processes found.\n"),count); } return 0; } void help() { tpf(_T("lp: List top-level proceses.\n")); tpf(_T(" Copyright 2002 Paul DiLascia.\n\n")); tpf(_T("Format: lp [/bct?]\n\n")); tpf(_T("Options:\n")); tpf(_T(" /b(are) no header\n")); tpf(_T(" /c(lass) show window class names\n")); tpf(_T(" /t(itle) show window titles\n")); tpf(_T("\n")); } |
所有的时间均为北京时间。 现在的时间是 06:12 PM. |