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

📄 processdlg1.cpp

📁 操作系统实验,进程控制实验,Process.exe为父进程执行程序ChildProcess.exe为子进程执行的程序 该程序实现了进程间的消息通信
💻 CPP
字号:
// ProcessDlg1.cpp : implementation file
//

#include "stdafx.h"
#include "Process.h"
#include "ProcessDlg1.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg dialog


CProcessDlg::CProcessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProcessDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProcessDlg)
	m_dwID = 0;
	m_dwPriortyClass = 0;
	m_dwYear = 0;
	m_dwMonth = 0;
	m_dwDay = 0;
	m_dwHour = 0;
	m_dwSecond = 0;
	m_dwMinute = 0;
	m_dwExitDay = 0;
	m_dwExitHour = 0;
	m_dwExitMinute = 0;
	m_dwExitSecond = 0;
	m_dwExitMonth = 0;
	m_dwVersion = 0;
	m_dwMinMemory = 0;
	m_dwMaxMemory = 0;
	m_dwMemory = 0;
	//}}AFX_DATA_INIT
}


void CProcessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProcessDlg)
	DDX_Text(pDX, IDC_EDIT1, m_dwID);
	DDX_Text(pDX, IDC_EDIT3, m_dwPriortyClass);
	DDX_Text(pDX, IDC_EDIT4, m_dwYear);
	DDX_Text(pDX, IDC_EDIT5, m_dwMonth);
	DDX_Text(pDX, IDC_EDIT6, m_dwDay);
	DDX_Text(pDX, IDC_EDIT7, m_dwHour);
	DDX_Text(pDX, IDC_EDIT9, m_dwSecond);
	DDX_Text(pDX, IDC_EDIT8, m_dwMinute);
	DDX_Text(pDX, IDC_EDIT10, m_dwVersion);
	DDX_Text(pDX, IDC_EDIT11, m_dwMinMemory);
	DDX_Text(pDX, IDC_EDIT12, m_dwMaxMemory);
	DDX_Text(pDX, IDC_EDIT2, m_dwMemory);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProcessDlg, CDialog)
	//{{AFX_MSG_MAP(CProcessDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnCreateProcess)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg message handlers
////////////////////////////////////////////////
int CDECL CProcessDlg::MessageBoxPrintf(TCHAR* szCaption,TCHAR*szFormat,...)
{
	TCHAR szBuffer[1024];
	va_list pArgList;
	va_start(pArgList,szFormat);
	_vsntprintf(szBuffer,sizeof(szBuffer)/sizeof(TCHAR),
		szFormat,pArgList);
	va_end(pArgList);

	return MessageBox(szBuffer,szCaption,0);
	///////////////////////////////
}
////////////////////////////////////////////////////
void CProcessDlg::OnCreateProcess() 
{
	// TODO: Add your control notification handler code here
	///////////创建进程////////
	BOOL fRet;
	memset(&si,0,sizeof(STARTUPINFO));
	ZeroMemory(&pi,sizeof(pi));

	DWORD nBufferLength;
	char cstrCurDir[256];
	DWORD dwResult=GetCurrentDirectory(nBufferLength,cstrCurDir);
	if(dwResult==0)
	{
		MessageBox("获得当前进程工作目录失败");
		exit(0);
	}
	CString strCurDir=cstrCurDir;
	CString strChildDir=strCurDir+"\\ChildProcess.exe";
	fRet=CreateProcess(strChildDir,
					   NULL,
					   NULL,
					   NULL,
					   TRUE,
					   0,
					   NULL,
					   NULL,
					   &si,
					   &pi);
	//MessageBoxPrintf(TEXT("HANDLE"),TEXT("si.hStdInput=%i"),si.hStdInput);
	if(!fRet)
	{
		MessageBox("进程创建失败","进程创建",0);
	}
	else
	{
		cpDlg.DoModal();
	}
}

BOOL CProcessDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_dwID=GetCurrentProcessId();
	m_hProc=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,m_dwID);
	m_dwPriortyClass=GetPriorityClass(m_hProc);

	/////////得到进程创建的时间///////////
	FILETIME ftCreationTime;
	FILETIME ftExitTime;
	FILETIME ftKernelTime;
	FILETIME ftUserTime; 
	BOOL bRtn=GetProcessTimes(m_hProc,           // specifies the process of interest
							  &ftCreationTime,    // when the process was created
							  &ftExitTime,        // when the process exited
                              &ftKernelTime,      // time the process has spent in kernel mode
                              &ftUserTime         // time the process has spent in user mode
							  );

	if(!bRtn)
	{
		MessageBox("GetProcessTimes调用失败!","ERROR",0);
	}

	SYSTEMTIME stCreationTime;
	
	FileTimeToSystemTime(&ftCreationTime,&stCreationTime);


	m_dwYear=stCreationTime.wYear;
	m_dwMonth=stCreationTime.wMonth;
	m_dwDay=stCreationTime.wDay;
	m_dwHour=stCreationTime.wHour;
	m_dwMinute=stCreationTime.wMinute;
	m_dwSecond=stCreationTime.wSecond;
	///////////得到进程的版本号//////////////
	m_dwVersion=GetProcessVersion(m_dwID);
	/////////得到进程所占内存的范围///////////
	GetProcessWorkingSetSize(m_hProc,  // open handle to the process of interest
							 &m_dwMinMemory,// points to variable to receive minimum working 
											// set size
							 &m_dwMaxMemory// points to variable to receive maximum working 
											// set size
											);
	//////////得到进程所占的实际内存//////////////
	PROCESS_MEMORY_COUNTERS psmemCounters;
	BOOL bResult=GetProcessMemoryInfo(m_hProc,  // handle to the process
							&psmemCounters,// structure that receives information
							sizeof(psmemCounters) // size of the structure
							);

	if(bResult==0)
	{
		MessageBoxPrintf("错误报告","GetProcessMemoryInfo出现错误:%d",
						GetLastError());
	}
	m_dwMemory=psmemCounters.WorkingSetSize;
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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