您的位置:首页 > 产品设计 > UI/UE

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

2009-12-08 14:57 267 查看
使用说明:

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

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

 

运行截图:



 

相关代码:


void CProcessTimeDlg::AddProcessToList(DWORD processID )
{
//
// Adds the process name and ID to the ListCtrl
//
// first update the process time
UpdateProcessTime();
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LSTPROCESS);
int nCount = pList->GetItemCount();

ST_PROCESSINFO *pstProcessInfo;
char szBuff[MAX_PATH];
char szProcessName[MAX_PATH] = "unknown";
// in case EnumProcessModules fails
char szItemString[MAX_PATH+64];
// open the process to query the time information
//   this handle will remain open until ClearProcessList call
//   This should remain open to get the process terminated time
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, processID);
if(!hProcess)
return;

for(int i=0;i<nCount; i++)
{
ST_PROCESSINFO *pstPrvProcessInfo =
(ST_PROCESSINFO *)pList->GetItemData(i);

// If the passed process id is same
// as the already updated process in the ListCtrl
//    then check whether it is a terminated process
//     if not then return immediately
//        without updating (to avoid flicker)
if(pstPrvProcessInfo->dwProcessId == processID)
{
CString cszText = pList->GetItemText(i,4);
cszText.TrimRight();
if(cszText == "")
return;
}
}

HMODULE hMod;
DWORD cbNeeded;
if(EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded))
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName));
wsprintf(szItemString, "%u", processID);
wsprintf(szBuff,"%d", nCount);
// fill the structure and store the info for later updates
pstProcessInfo        = new ST_PROCESSINFO();
pstProcessInfo->dwProcessId    = processID;
pstProcessInfo->hProcess    = hProcess;
pList->InsertItem(nCount,szItemString);
pList->SetItemText(nCount,2,szProcessName);
pList->SetItemText(nCount,1,szBuff);
pList->SetItemData(nCount,(DWORD)pstProcessInfo);
}


 

// this will update the process start time and end time
// (end time, only if the process has terminated)
void CProcessTimeDlg::UpdateProcessTime()
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LSTPROCESS);
FILETIME ftCreate, ftExit, ftKernel, ftUser;

int nCount = pList->GetItemCount();
// loop all the process in the list box
for(int i=0;i<nCount;i++)
{
ST_PROCESSINFO *pstProcessInfo =
(ST_PROCESSINFO *)pList->GetItemData(i);
if(!pstProcessInfo->hProcess)
continue;
if(GetProcessTimes(pstProcessInfo->hProcess,
&ftCreate, &ftExit, &ftKernel, &ftUser))
{
// Horrible, disgusting hack!
// The two lines below basically grab the
// contents of a FILETIME structure
// and store it in a 64 bit integer.
LONGLONG tUser64 = *(LONGLONG *)&ftUser;
LONGLONG tKernel64 = *(LONGLONG *)&ftKernel;

DWORD tUser, tKernel;
// The LONGLONGs contain the time in 100 nanosecond intervals (now
// there's a useful unit of measurement...).  Divide each of them by
// 10000 to convert into milliseconds, and store the results in a
// DWORD.  This means that the max time before overflowing is around
// 4 Million seconds (about 49 days)
tUser = (DWORD)(tUser64 / 10000);
tKernel = (DWORD)(tKernel64 / 10000);

// Format the user and kernel times, and add to the process node
char szItem[128];
char szFileDate[32] = { 0 };
char szFileTime[32] = { 0 };
if(!ftCreate.dwHighDateTime&&!ftCreate.dwLowDateTime)
{
strcpy(szFileDate,"");
strcpy(szFileTime,"");
}
else
{    // formatting the date & time
GetFileDateAsString(&ftCreate, szFileDate, sizeof(szFileDate));
GetFileTimeAsString(&ftCreate, szFileTime, sizeof(szFileTime));
}
wsprintf(szItem, "%s %s", szFileDate, szFileTime);
CString cszText = pList->GetItemText(i,3);
// if already exists then don't update, this will reduce the flicker
if(cszText != szItem)
pList->SetItemText(i,3,szItem);
if(!ftExit.dwHighDateTime&&!ftExit.dwLowDateTime)
{
strcpy(szFileDate,"");
strcpy(szFileTime,"");
}
else
{    // formatting the date & time
GetFileDateAsString(&ftExit, szFileDate, sizeof(szFileDate));
GetFileTimeAsString(&ftExit, szFileTime, sizeof(szFileTime));
}
wsprintf(szItem, "%s %s", szFileDate, szFileTime);
cszText = pList->GetItemText(i,4);
// if already exists then don't update, this will reduce the flicker
if(cszText != szItem)
pList->SetItemText(i,4,szItem);
}
}
}


 

下载地址:




 

http://www.codeproject.com/KB/threads/ProcessTime/ProcessTime_src.zip

 

http://download.csdn.net/source/1883332

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息