MemLookup v1.0
用于查看程序运行内存
写的很糟糕,而且似乎单位不准,结果仅供参考。
- 编译选项 -lpsapi
- 版本低的貌似需要改一下Psapi.h?(加一个#include<windows.h>,不然会有编译错误)
#include<bits/stdc++.h>
#include<Psapi.h>
#include<tlhelp32.h>
#pragma comment(lib,"Psapi.lib")
using namespace std;
int slp=0;
void GetSystem() {
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, false, &tkp, sizeof tkp, NULL, NULL);
CloseHandle(hToken);
}
DWORD GetProcessidFromName(char *name) {
PROCESSENTRY32 pe;
DWORD id=0;
HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
pe.dwSize=sizeof(PROCESSENTRY32);
if(!Process32First(hSnapshot,&pe))
return 0;
while(1) {
pe.dwSize=sizeof(PROCESSENTRY32);
if(Process32Next(hSnapshot,&pe)==FALSE)
break;
if(strcmp(pe.szExeFile,name)==0) {
id=pe.th32ProcessID;
break;
}
}
CloseHandle(hSnapshot);
return id;
}
void killman(char *ProcessName) {
// cout<<ProcessName<<endl;
HANDLE hProcess=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
HANDLE hModule;
PROCESSENTRY32 pinfo;
MODULEENTRY32 minfo;
char shortpath[256];
pinfo.dwSize = sizeof( PROCESSENTRY32 );
BOOL report =Process32First(hProcess,&pinfo);
while(report) {
hModule=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pinfo.th32ProcessID);
Module32First(hModule, &minfo);
GetShortPathName(minfo.szExePath,shortpath,256);
if(!(strcmp(pinfo.szExeFile,ProcessName))) {
size_t maxuse=0,last=0,last2=0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pinfo.th32ProcessID );
while(GetProcessidFromName(ProcessName)) {
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
if(last!=pmc.PeakWorkingSetSize||last2!=pmc.WorkingSetSize){
system("cls");
printf("监视中...\n");
printf("目标[%s] PID[%lld] HANDLE[",ProcessName,pinfo.th32ProcessID);
cout<<hex<<hProcess<<"]\n";
printf("当前顶峰运行内存%lldB[%lldMB][%lldKB]\n",pmc.PeakWorkingSetSize,pmc.PeakWorkingSetSize/1024/1024,pmc.PeakWorkingSetSize/1024);
maxuse=max(maxuse,pmc.PeakWorkingSetSize);
printf("当前运行内存%lldB[%lldMB][%lldKB]\n",pmc.WorkingSetSize,pmc.WorkingSetSize/1024/1024,pmc.WorkingSetSize/1024);
maxuse=max(maxuse,pmc.WorkingSetSize);
last=pmc.PeakWorkingSetSize;last2=pmc.WorkingSetSize;
}
Sleep(slp);
}
printf("\n结果:\n运行已结束,运行过程中最高使用内存%lldB[%lldMB][%lldKB]\n",maxuse,maxuse/1024/1024,maxuse/1024);
break;
}
// AfxMessageBox(pinfo.szExeFile);
report =Process32Next(hProcess, &pinfo);
}
}
int main() {
system("title MemLookup 运行内存监视器");
system("mode con cols=60 lines=5");
SetWindowPos(GetConsoleWindow(),HWND_TOPMOST,699,496,0,0,SWP_NOSIZE|SWP_SHOWWINDOW);
GetSystem();
printf("进程映像名:");
char a[10000];
cin>>a;
string l,t=a;
l="title MemLookup 运行内存监视器 -\"";
l+=t;
l+="\"";
system(l.c_str());
printf("等待毫秒数:");
cin>>slp;
slp=abs(slp);
killman(a);
system("pause");
return 0;
}
标签:Memlookup,slp,tkp,pe,hToken,1.0,include,hSnapshot
From: https://www.cnblogs.com/haozexu/p/17488421.html