ProcessInfoDlg.cpp
上传用户:guming
上传日期:2022-08-08
资源大小:5694k
文件大小:7k
源码类别:

进程与线程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "ProcessInfo.h"
  3. #include "ProcessInfoDlg.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. extern CString PName;
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CProcessInfoDlg dialog
  12. CProcessInfoDlg::CProcessInfoDlg(CWnd* pParent /*=NULL*/)
  13. : CDialog(CProcessInfoDlg::IDD, pParent)
  14. {
  15. //{{AFX_DATA_INIT(CProcessInfoDlg)
  16. //}}AFX_DATA_INIT
  17. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  18. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  19. }
  20. void CProcessInfoDlg::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. //{{AFX_DATA_MAP(CProcessInfoDlg)
  24. DDX_Control(pDX, IDC_EDIT1, m_Edit1);
  25. DDX_Control(pDX, IDC_LC_PROCESSINFO, m_lcProcessInfo);
  26. //}}AFX_DATA_MAP
  27. }
  28. BEGIN_MESSAGE_MAP(CProcessInfoDlg, CDialog)
  29. //{{AFX_MSG_MAP(CProcessInfoDlg)
  30. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  31. ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
  32. ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CProcessInfoDlg message handlers
  37. BOOL CProcessInfoDlg::OnInitDialog()
  38. {
  39. CDialog::OnInitDialog();
  40. // Set the icon for this dialog.  The framework does this automatically
  41. //  when the application's main window is not a dialog
  42. SetIcon(m_hIcon, TRUE); // Set big icon
  43. SetIcon(m_hIcon, FALSE); // Set small icon
  44.     m_lcProcessInfo.InsertColumn(0, "进程ID",        LVCFMT_LEFT,  50);
  45.     m_lcProcessInfo.InsertColumn(1, "进程名称",          LVCFMT_LEFT, 130);
  46.     m_lcProcessInfo.InsertColumn(2, "创建时间", LVCFMT_LEFT, 140);
  47.     m_lcProcessInfo.InsertColumn(3, "运行时间",  LVCFMT_LEFT, 100);
  48.     
  49.     m_lcProcessInfo.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  50.    
  51.     return (TRUE);
  52. }
  53. //====================================================================
  54. void CProcessInfoDlg::Process(void)
  55. {    
  56.     DWORD               dwProcessIDs[150],
  57.                         dwSize,
  58.                         dwNeeded;
  59.     HANDLE              hProcess;
  60.     HMODULE             hModuleHandle;
  61.     FILETIME            ftCreation,
  62.                         ftExit,
  63.                         ftKernel,
  64.                         ftUser;
  65.     SYSTEMTIME          stKernel,
  66.                         stUser;
  67.     CString             strName,
  68.                         strData1,
  69. strData2,
  70. strData3,
  71. a("n");
  72.     int                 nIndex,
  73.                         nProcessCount,
  74.                         nItem;
  75.     COleDateTime        timeCreation,
  76.                         timeNow;
  77.     COleDateTimeSpan    timeDiff;
  78. FILE *pFile=fopen("进程信息.txt","w");
  79.     if (EnumProcesses(dwProcessIDs, sizeof(dwProcessIDs), &dwSize) == TRUE)
  80.     {
  81.         // get the current time so we can calculate running time later
  82.         timeNow = COleDateTime::GetCurrentTime();//获取当前时间
  83.         nProcessCount = dwSize / sizeof(DWORD);
  84.         for (nIndex = 0; nIndex < nProcessCount; nIndex++)
  85.         {
  86.           
  87. hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwProcessIDs[nIndex]);
  88.             if (NULL != hProcess)
  89.             {
  90.                
  91. strData1.Format("%04d", dwProcessIDs[nIndex]);
  92.                 nItem = m_lcProcessInfo.InsertItem(nIndex, strData1);//InsterItem
  93.                 if (EnumProcessModules(hProcess, &hModuleHandle, sizeof(hModuleHandle), &dwNeeded) == TRUE)
  94.                 {
  95.                     if (GetModuleBaseName(hProcess, hModuleHandle, strName) > 0) //获取进程名
  96.                     {
  97.                       
  98. m_lcProcessInfo.SetItemText(nItem, 1, strName);
  99.                         if (GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser) == TRUE)
  100.                         {
  101.                             timeCreation = ftCreation;
  102. strData2.Format("%02d:%02d:%02d @ %02d/%02d/%04d",
  103.                                 timeCreation.GetHour(),
  104.                                 timeCreation.GetMinute(),
  105.                                 timeCreation.GetSecond(), 
  106.                                 timeCreation.GetMonth(),
  107.                                 timeCreation.GetDay(),
  108.                                 timeCreation.GetYear());
  109.                             m_lcProcessInfo.SetItemText(nItem, 2, strData2);                           
  110.                             
  111.                             timeDiff = timeNow - timeCreation;
  112.                             strData3.Format("%ud %uh %um %us", timeDiff.GetDays(), timeDiff.GetHours(), timeDiff.GetMinutes(), timeDiff.GetSeconds());
  113.                             m_lcProcessInfo.SetItemText(nItem, 3, strData3);                                                        
  114.                             
  115. strName  = strName + "  ";
  116. strData1 = strData1 + "  ";
  117. strData2 = strData2 + "  ";
  118. strData3 = strData3 + "n";
  119. fwrite(strData1,1,strlen(strData1),pFile);
  120. fwrite(strName,1,strlen(strName),pFile);
  121. fwrite(strData2,1,strlen(strData2),pFile);
  122. fwrite(strData3,1,strlen(strData3),pFile);
  123.                            
  124.                         }
  125.                         else
  126.                             TRACE("GetProcessTimes() failed: %sn", GetErrorMessage());
  127.                     }
  128.                     else
  129.                         TRACE("GetModuleBaseName() failed: %sn", GetErrorMessage());
  130.                 }
  131.                 else
  132.                     TRACE2("EnumProcessModules() failed on PID %#x: %sn", dwProcessIDs[nIndex], GetErrorMessage());
  133.                 CloseHandle(hProcess);
  134.             }
  135.             else
  136.                 TRACE2("OpenProcess(%#x) failed: %sn", dwProcessIDs[nIndex], GetErrorMessage());
  137.         }
  138.     }
  139.     else
  140.         TRACE("EnumProcesses() failed: %sn", GetErrorMessage());
  141. fclose(pFile);
  142. }
  143. //====================================================================
  144. DWORD CProcessInfoDlg::GetModuleBaseName( const HANDLE hProcess, const HMODULE hModule, CString &strName )
  145. {
  146.     DWORD   dwLength;
  147.     dwLength = ::GetModuleBaseName(hProcess, hModule, strName.GetBuffer(MAX_PATH), MAX_PATH);
  148.     strName.ReleaseBuffer();
  149.     return (dwLength);
  150. }
  151. //====================================================================
  152. CString CProcessInfoDlg::GetErrorMessage( void )
  153. {
  154.     CString strError;
  155.     LPVOID  lpMsgBuf;
  156.     FormatMessage( 
  157.         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
  158.         FORMAT_MESSAGE_FROM_SYSTEM | 
  159.         FORMAT_MESSAGE_IGNORE_INSERTS,
  160.         NULL,
  161.         GetLastError(),
  162.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  163.         (LPTSTR) &lpMsgBuf,
  164.         0,
  165.         NULL);
  166.     strError = (LPTSTR) lpMsgBuf;
  167.     LocalFree(lpMsgBuf);
  168.     return (strError);
  169. }
  170. void CProcessInfoDlg::OnButton1() 
  171. {
  172. // TODO: Add your control notification handler code here 选择Edit1中的全部,付给
  173. Process();
  174. }
  175. void CProcessInfoDlg::OnCancel() 
  176. {
  177. // TODO: Add extra cleanup here
  178. CDialog::OnCancel();
  179. }
  180. void CProcessInfoDlg::OnSetfocusEdit1() 
  181. {
  182. IfEdit1=TRUE;
  183. }
  184. void CProcessInfoDlg::OnChangeEdit1() 
  185. {
  186. m_Edit1.GetWindowText(PName);
  187. }