⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 process.cpp

📁 创建一个进程并获得进程的 pid 运行时间 并结束创建的进程。
💻 CPP
字号:

#include <iostream>
#include<windows.h>
using namespace std;
int main()
{
	SIZE_T  n1,n2,n3,m_uiCpuTime;     
	SYSTEMTIME st1,st2;   
	FILETIME  ft1,ft2,ft3,ft4;   
	HANDLE hProcess;

	STARTUPINFO si; //一些必备参数设置
	memset(&si, 0, sizeof(STARTUPINFO)); 
	si.cb = sizeof(STARTUPINFO); 
	si.dwFlags = STARTF_USESHOWWINDOW; 
	si.wShowWindow = SW_SHOW; 



	PROCESS_INFORMATION pi; //必备参数设置结束
	if(!CreateProcess(NULL,"cmd.exe",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) //"cmd.exe"是您要运行的程序的路径
	{
		cout<<"Create Fail!"<<endl;
		exit(1);
	}
	else
	{	
		hProcess=GetCurrentProcess();
			
          //   获得进程使用的CPU时间(WINDOWS的任务管理器使用的是   内核时间+用户时间)   
          if(GetProcessTimes(hProcess,&ft1,&ft2,&ft3,&ft4))   
          {		
				cout<<"success01!"<<endl;
				cout<<pi.dwProcessId<<endl;
				cout<<pi.dwThreadId<<endl;
				cout<<pi.hProcess<<endl;
				cout<<pi.hThread<<endl;
				if(FileTimeToSystemTime(&ft3,&st1))   
				{   
				  cout<<"success02!"<<endl;
                  //   把进程以用户模式已经执行的时间转换为文件时间   
                  if   (FileTimeToSystemTime(&ft4,&st2))   
                  {   
					  n1   =   st1.wHour   +   st2.wHour;   
                      n2   =   st1.wMinute   +   st2.wMinute;   
                      n3   =   st1.wSecond   +   st2.wSecond;   
    
                      //   保存进程所使用的CPU时间(单位:秒)到m_uiCpuTime变量里   
                      m_uiCpuTime   =   n1   *   3600   +   n2   *   60   +   n3;  
					  cout<<m_uiCpuTime<<endl;
					  cout<<st1.wHour<<"\t"<<":"<<st1.wMinute<<"\t"<<":"<<st1.wMilliseconds<<endl;
                      cout<<st2.wHour<<"\t"<<":"<<st2.wMinute<<"\t"<<":"<<st2.wMilliseconds<<endl;
					  cout<<"success!03"<<endl;
								
                   }   
               }   
          }   
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -