ProcessInfoDlg.cpp
上传用户:guming
上传日期:2022-08-08
资源大小:5694k
文件大小:7k
- #include "stdafx.h"
- #include "ProcessInfo.h"
- #include "ProcessInfoDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CString PName;
- /////////////////////////////////////////////////////////////////////////////
- // CProcessInfoDlg dialog
- CProcessInfoDlg::CProcessInfoDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CProcessInfoDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CProcessInfoDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CProcessInfoDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CProcessInfoDlg)
- DDX_Control(pDX, IDC_EDIT1, m_Edit1);
- DDX_Control(pDX, IDC_LC_PROCESSINFO, m_lcProcessInfo);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CProcessInfoDlg, CDialog)
- //{{AFX_MSG_MAP(CProcessInfoDlg)
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
- ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CProcessInfoDlg message handlers
- BOOL CProcessInfoDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- m_lcProcessInfo.InsertColumn(0, "进程ID", LVCFMT_LEFT, 50);
- m_lcProcessInfo.InsertColumn(1, "进程名称", LVCFMT_LEFT, 130);
- m_lcProcessInfo.InsertColumn(2, "创建时间", LVCFMT_LEFT, 140);
- m_lcProcessInfo.InsertColumn(3, "运行时间", LVCFMT_LEFT, 100);
-
- m_lcProcessInfo.SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
- return (TRUE);
- }
- //====================================================================
- void CProcessInfoDlg::Process(void)
- {
- DWORD dwProcessIDs[150],
- dwSize,
- dwNeeded;
- HANDLE hProcess;
- HMODULE hModuleHandle;
- FILETIME ftCreation,
- ftExit,
- ftKernel,
- ftUser;
- SYSTEMTIME stKernel,
- stUser;
- CString strName,
- strData1,
- strData2,
- strData3,
- a("n");
- int nIndex,
- nProcessCount,
- nItem;
- COleDateTime timeCreation,
- timeNow;
- COleDateTimeSpan timeDiff;
- FILE *pFile=fopen("进程信息.txt","w");
-
- if (EnumProcesses(dwProcessIDs, sizeof(dwProcessIDs), &dwSize) == TRUE)
- {
- // get the current time so we can calculate running time later
- timeNow = COleDateTime::GetCurrentTime();//获取当前时间
- nProcessCount = dwSize / sizeof(DWORD);
- for (nIndex = 0; nIndex < nProcessCount; nIndex++)
- {
-
- hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwProcessIDs[nIndex]);
- if (NULL != hProcess)
- {
-
- strData1.Format("%04d", dwProcessIDs[nIndex]);
- nItem = m_lcProcessInfo.InsertItem(nIndex, strData1);//InsterItem
- if (EnumProcessModules(hProcess, &hModuleHandle, sizeof(hModuleHandle), &dwNeeded) == TRUE)
- {
- if (GetModuleBaseName(hProcess, hModuleHandle, strName) > 0) //获取进程名
- {
-
- m_lcProcessInfo.SetItemText(nItem, 1, strName);
-
- if (GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser) == TRUE)
- {
- timeCreation = ftCreation;
- strData2.Format("%02d:%02d:%02d @ %02d/%02d/%04d",
- timeCreation.GetHour(),
- timeCreation.GetMinute(),
- timeCreation.GetSecond(),
- timeCreation.GetMonth(),
- timeCreation.GetDay(),
- timeCreation.GetYear());
- m_lcProcessInfo.SetItemText(nItem, 2, strData2);
-
- timeDiff = timeNow - timeCreation;
- strData3.Format("%ud %uh %um %us", timeDiff.GetDays(), timeDiff.GetHours(), timeDiff.GetMinutes(), timeDiff.GetSeconds());
- m_lcProcessInfo.SetItemText(nItem, 3, strData3);
-
- strName = strName + " ";
- strData1 = strData1 + " ";
- strData2 = strData2 + " ";
- strData3 = strData3 + "n";
-
- fwrite(strData1,1,strlen(strData1),pFile);
- fwrite(strName,1,strlen(strName),pFile);
- fwrite(strData2,1,strlen(strData2),pFile);
- fwrite(strData3,1,strlen(strData3),pFile);
-
- }
- else
- TRACE("GetProcessTimes() failed: %sn", GetErrorMessage());
- }
- else
- TRACE("GetModuleBaseName() failed: %sn", GetErrorMessage());
- }
- else
- TRACE2("EnumProcessModules() failed on PID %#x: %sn", dwProcessIDs[nIndex], GetErrorMessage());
- CloseHandle(hProcess);
- }
- else
- TRACE2("OpenProcess(%#x) failed: %sn", dwProcessIDs[nIndex], GetErrorMessage());
- }
- }
- else
- TRACE("EnumProcesses() failed: %sn", GetErrorMessage());
- fclose(pFile);
- }
- //====================================================================
- DWORD CProcessInfoDlg::GetModuleBaseName( const HANDLE hProcess, const HMODULE hModule, CString &strName )
- {
- DWORD dwLength;
- dwLength = ::GetModuleBaseName(hProcess, hModule, strName.GetBuffer(MAX_PATH), MAX_PATH);
- strName.ReleaseBuffer();
- return (dwLength);
- }
- //====================================================================
- CString CProcessInfoDlg::GetErrorMessage( void )
- {
- CString strError;
- LPVOID lpMsgBuf;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL);
- strError = (LPTSTR) lpMsgBuf;
- LocalFree(lpMsgBuf);
- return (strError);
- }
- void CProcessInfoDlg::OnButton1()
- {
- // TODO: Add your control notification handler code here 选择Edit1中的全部,付给
- Process();
- }
- void CProcessInfoDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CDialog::OnCancel();
- }
- void CProcessInfoDlg::OnSetfocusEdit1()
- {
- IfEdit1=TRUE;
- }
- void CProcessInfoDlg::OnChangeEdit1()
- {
- m_Edit1.GetWindowText(PName);
- }