VC获取进程启动和结束时间

来源:互联网 发布:arm linux gcc下载 编辑:程序博客网 时间:2024/04/29 02:54

使用说明:

该工具来自 codeproject,使用 PSAPI 获取内核模式和用户模式进程并将用户模式进程的启动时间和结束时间记入日志。本文工具程序名为 ProcessTime。运行程序后,它启动一个线程,该线程在后台执行每隔一定时间监控是否有新启动进程或退出进程。

英文信息请参考:http://www.codeproject.com/threads/ProcessTime.asp

 

运行截图:

 

相关代码:

[cpp] view plaincopyprint?
  1. void CProcessTimeDlg::AddProcessToList(DWORD processID )  
  2. {  
  3.     //  
  4.     // Adds the process name and ID to the ListCtrl  
  5.     //  
  6.     // first update the process time   
  7.     UpdateProcessTime();  
  8.     CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LSTPROCESS);  
  9.     int nCount = pList->GetItemCount();  
  10.       
  11.     ST_PROCESSINFO *pstProcessInfo;  
  12.     char szBuff[MAX_PATH];  
  13.     char szProcessName[MAX_PATH] = "unknown";  
  14.     // in case EnumProcessModules fails  
  15.     char szItemString[MAX_PATH+64];  
  16.     // open the process to query the time information  
  17.     //   this handle will remain open until ClearProcessList call  
  18.     //   This should remain open to get the process terminated time   
  19.     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,   
  20.                                   FALSE, processID);  
  21.     if(!hProcess)  
  22.         return;  
  23.       
  24.     for(int i=0;i<nCount; i++)  
  25.     {  
  26.         ST_PROCESSINFO *pstPrvProcessInfo =   
  27.                        (ST_PROCESSINFO *)pList->GetItemData(i);  
  28.           
  29.         // If the passed process id is same  
  30.         // as the already updated process in the ListCtrl  
  31.         //    then check whether it is a terminated process  
  32.         //     if not then return immediately  
  33.         //        without updating (to avoid flicker)  
  34.         if(pstPrvProcessInfo->dwProcessId == processID)  
  35.         {  
  36.             CString cszText = pList->GetItemText(i,4);  
  37.             cszText.TrimRight();  
  38.             if(cszText == "")  
  39.                 return;  
  40.         }  
  41.     }  
  42.       
  43.     HMODULE hMod;  
  44.     DWORD cbNeeded;      
  45.     if(EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded))  
  46.         GetModuleBaseName( hProcess, hMod, szProcessName,   
  47.                            sizeof(szProcessName));  
  48.     wsprintf(szItemString, "%u", processID);  
  49.     wsprintf(szBuff,"%d", nCount);  
  50.     // fill the structure and store the info for later updates  
  51.     pstProcessInfo        = new ST_PROCESSINFO();  
  52.     pstProcessInfo->dwProcessId    = processID;  
  53.     pstProcessInfo->hProcess    = hProcess;  
  54.     pList->InsertItem(nCount,szItemString);  
  55.     pList->SetItemText(nCount,2,szProcessName);  
  56.     pList->SetItemText(nCount,1,szBuff);  
  57.     pList->SetItemData(nCount,(DWORD)pstProcessInfo);  
  58. }  

 

[cpp] view plaincopyprint?
  1. // this will update the process start time and end time  
  2. // (end time, only if the process has terminated)  
  3. void CProcessTimeDlg::UpdateProcessTime()  
  4. {  
  5.     CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LSTPROCESS);  
  6.     FILETIME ftCreate, ftExit, ftKernel, ftUser;  
  7.       
  8.     int nCount = pList->GetItemCount();  
  9.     // loop all the process in the list box  
  10.     for(int i=0;i<nCount;i++)  
  11.     {  
  12.         ST_PROCESSINFO *pstProcessInfo =   
  13.                        (ST_PROCESSINFO *)pList->GetItemData(i);  
  14.         if(!pstProcessInfo->hProcess)  
  15.             continue;  
  16.         if(GetProcessTimes(pstProcessInfo->hProcess,   
  17.                            &ftCreate, &ftExit, &ftKernel, &ftUser))  
  18.         {  
  19.             // Horrible, disgusting hack!  
  20.             // The two lines below basically grab the  
  21.             // contents of a FILETIME structure  
  22.             // and store it in a 64 bit integer.  
  23.             LONGLONG tUser64 = *(LONGLONG *)&ftUser;  
  24.             LONGLONG tKernel64 = *(LONGLONG *)&ftKernel;  
  25.       
  26.             DWORD tUser, tKernel;  
  27.             // The LONGLONGs contain the time in 100 nanosecond intervals (now  
  28.             // there's a useful unit of measurement...).  Divide each of them by  
  29.             // 10000 to convert into milliseconds, and store the results in a  
  30.             // DWORD.  This means that the max time before overflowing is around  
  31.             // 4 Million seconds (about 49 days)  
  32.             tUser = (DWORD)(tUser64 / 10000);  
  33.             tKernel = (DWORD)(tKernel64 / 10000);  
  34.               
  35.             // Format the user and kernel times, and add to the process node  
  36.             char szItem[128];  
  37.             char szFileDate[32] = { 0 };  
  38.             char szFileTime[32] = { 0 };  
  39.             if(!ftCreate.dwHighDateTime&&!ftCreate.dwLowDateTime)  
  40.             {  
  41.                 strcpy(szFileDate,"");  
  42.                 strcpy(szFileTime,"");  
  43.             }  
  44.             else  
  45.             {    // formatting the date & time  
  46.                 GetFileDateAsString(&ftCreate, szFileDate, sizeof(szFileDate));  
  47.                 GetFileTimeAsString(&ftCreate, szFileTime, sizeof(szFileTime));  
  48.             }  
  49.             wsprintf(szItem, "%s %s", szFileDate, szFileTime);  
  50.             CString cszText = pList->GetItemText(i,3);  
  51.             // if already exists then don't update, this will reduce the flicker  
  52.             if(cszText != szItem)  
  53.                 pList->SetItemText(i,3,szItem);  
  54.             if(!ftExit.dwHighDateTime&&!ftExit.dwLowDateTime)  
  55.             {  
  56.                 strcpy(szFileDate,"");  
  57.                 strcpy(szFileTime,"");  
  58.             }  
  59.             else  
  60.             {    // formatting the date & time  
  61.                 GetFileDateAsString(&ftExit, szFileDate, sizeof(szFileDate));  
  62.                 GetFileTimeAsString(&ftExit, szFileTime, sizeof(szFileTime));  
  63.             }  
  64.             wsprintf(szItem, "%s %s", szFileDate, szFileTime);  
  65.             cszText = pList->GetItemText(i,4);  
  66.             // if already exists then don't update, this will reduce the flicker  
  67.             if(cszText != szItem)  
  68.                 pList->SetItemText(i,4,szItem);  
  69.         }  
  70.     }  
  71. }  

 

下载地址:

0 0