目录
获取进程信息
进程信息包括 进程ID,进程名,进程的持续时间,以及进程的内存使用情况。
其中,进程的持续时间 = 系统启动时间 - 进程启动时间,因为得到的时间都是SystemTime(UTC格林尼治时间,与北京时间相差8个小时),所以还需要对其小时数加8模24(只是一个简单地处理,如果电脑很早之前就打开了,还需要对其年、月、日进行修改)。
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include <iostream>
#include <time.h>
using namespace std;
#pragma comment(lib, "psapi.lib")
// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1
bool AdjustPurview()//提升进程权限
{
TOKEN_PRIVILEGES TokenPrivileges;
BOOL bRet;
HANDLE hToken;
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &TokenPrivileges.Privileges[0].Luid);
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
TokenPrivileges.PrivilegeCount = 1;
TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
bRet = AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, 0, NULL, NULL);
CloseHandle(hToken);
return bRet == TRUE;
}
void PrintMemoryInfo(DWORD processID)
{
TCHAR szProcessName[MAX_PATH];//用来存放进程名
AdjustPurview();
//取得进程的句柄
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
LPSYSTEMTIME endTime = new SYSTEMTIME;
::GetSystemTime(endTime);
endTime->wHour = (endTime->wHour + 8) % 24;
DWORD dwEndTime =
endTime->wHour * 60 * 60 * 1000
+ endTime->wMinute * 60 * 1000
+ endTime->wSecond * 1000
+ endTime->wMilliseconds;
/*printf("\nProcess ID: %u\n", processID);*/
// Print information about the memory usage of the process.
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (NULL == hProcess)
return;
FILETIME starttime, exittime, ipker, ipuser;//用来记录启动时间,结束时间,进程在核心模式下消耗的时间,进程在用户模式下消耗的时间
LPSYSTEMTIME lpSystemTime = new SYSTEMTIME;//用来将filetime转换为systemtime[systemtime可以让人看懂]
GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));//获取进程
GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH);//获取进程名
GetProcessTimes(hProcess, &starttime, &exittime, &ipker, &ipuser);//获取进程时间
printf(" 进程ID号:%d\n", processID);
printf(" 进程名称:%ws\n", szProcessName);
//printf(" 峰值内存使用: %lldMB\n", pmc.PeakWorkingSetSize/(1024*1024));
printf(" 内存使用: %dK\n", pmc.WorkingSetSize / 1024);
FileTimeToSystemTime(&starttime, lpSystemTime);
/*printf(" 进程建立的时间: ");
cout << lpSystemTime->wYear << "-" << lpSystemTime->wMonth << "-" << lpSystemTime->wDay << endl;*/
printf(" 进程持续时间: ");
lpSystemTime->wHour = (lpSystemTime->wHour + 8) % 24;//将UTC时间化为北京时间,这里只转化了小时,如果有需要可以转化年、月、日
DWORD dwBeginTime =
lpSystemTime->wHour * 60 * 60 * 1000
+ lpSystemTime->wMinute * 60 * 1000
+ lpSystemTime->wSecond * 1000
+ lpSystemTime->wMilliseconds;
cout << (dwEndTime - dwBeginTime) / 1000 / 60 << " min" << endl;
//cout << lpSystemTime->wHour << ":" << lpSystemTime->wMinute << ":" << lpSystemTime->wSecond << endl << endl;
FileTimeToSystemTime(&exittime, lpSystemTime);
lpSystemTime->wHour = (lpSystemTime->wHour + 8) % 24;
DWORD dwExitTime =
lpSystemTime->wHour * 60 * 60 * 1000
+ lpSystemTime->wMinute * 60 * 1000
+ lpSystemTime->wSecond * 1000
+ lpSystemTime->wMilliseconds;
printf(" 进程结束的时间: ");
//cout << (dwEndTime - dwBeginTime) / 1000 / 60 << " min" << endl << endl;
cout << lpSystemTime->wYear << "-" << lpSystemTime->wMonth << "-" << lpSystemTime->wDay << " " << lpSystemTime->wHour << ":" << lpSystemTime->wMinute << ":" << lpSystemTime->wSecond << endl << endl;
//printf(" 虚拟内存使用: %lldMB\n\n", pmc.PagefileUsage / (1024 * 1024));
//printf("\t峰值虚拟内存使用: %d\n",pmc.PeakPagefileUsage / (1024 * 1024));
CloseHandle(hProcess);
}
int main(void)
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
// Get the list of process identifiers.
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the memory usage for each process
for (i = 0; i < cProcesses; i++)
{
PrintMemoryInfo(aProcesses[i]);
}
return 0;
}
GetLocalTime()函数可以获取北京时间