📄 childprocess.cpp
字号:
// ChildProcess.cpp : implementation file
//
#include "stdafx.h"
#include "Process.h"
#include "ChildProcess.h"
#include "ProcessDlg1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChildProcess dialog
int CDECL CChildProcess::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);
///////////////////////////////
}
CChildProcess::CChildProcess(CWnd* pParent /*=NULL*/)
: CDialog(CChildProcess::IDD, pParent)
{
//{{AFX_DATA_INIT(CChildProcess)
m_dwCreateYear = 0;
m_dwCreateMonth = 0;
m_dwCreateDay = 0;
m_dwCreateHour = 0;
m_dwCreateMinute = 0;
m_dwCreateSecond = 0;
m_dwExitYear = 0;
m_dwExitMonth = 0;
m_dwExitDay = 0;
m_dwExitHour = 0;
m_dwExitMinute = 0;
m_dwExitSecond = 0;
m_dwID = 0;
m_dwVersion = 0;
m_dwMinMemory = 0;
m_dwMaxMemory = 0;
m_dwPriorityClass = 0;
m_strMessage = _T("");
m_dwMemory = 0;
//}}AFX_DATA_INIT
}
void CChildProcess::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChildProcess)
DDX_Text(pDX, IDC_EDIT4, m_dwCreateYear);
DDX_Text(pDX, IDC_EDIT5, m_dwCreateMonth);
DDX_Text(pDX, IDC_EDIT6, m_dwCreateDay);
DDX_Text(pDX, IDC_EDIT7, m_dwCreateHour);
DDX_Text(pDX, IDC_EDIT8, m_dwCreateMinute);
DDX_Text(pDX, IDC_EDIT9, m_dwCreateSecond);
DDX_Text(pDX, IDC_EDIT13, m_dwExitYear);
DDX_Text(pDX, IDC_EDIT14, m_dwExitMonth);
DDX_Text(pDX, IDC_EDIT15, m_dwExitDay);
DDX_Text(pDX, IDC_EDIT16, m_dwExitHour);
DDX_Text(pDX, IDC_EDIT17, m_dwExitMinute);
DDX_Text(pDX, IDC_EDIT18, m_dwExitSecond);
DDX_Text(pDX, IDC_EDIT1, m_dwID);
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_EDIT3, m_dwPriorityClass);
DDX_Text(pDX, IDC_EDIT19, m_strMessage);
DDX_Text(pDX, IDC_EDIT2, m_dwMemory);
//}}AFX_DATA_MAP
}
//const UINT wm_Receive=::RegisterWindowMessage("ReceiveMessage");
BEGIN_MESSAGE_MAP(CChildProcess, CDialog)
//{{AFX_MSG_MAP(CChildProcess)
ON_BN_CLICKED(IDOK, OnEndProcess)
ON_BN_CLICKED(IDC_BUTTON2, OnSendInfo)
ON_BN_CLICKED(IDC_BUTTON1, OnWritePipe)
ON_BN_CLICKED(IDC_BUTTON3, OnWriteFileMap)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChildProcess message handlers
BOOL CChildProcess::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CProcessDlg* prodlg=(CProcessDlg*)AfxGetApp()->GetMainWnd();
m_dwID=prodlg->pi.dwProcessId;
m_dwPriorityClass=GetPriorityClass(prodlg->pi.hProcess);
FILETIME ftCreationTime;
FILETIME ftExitTime;
FILETIME ftKernelTime;
FILETIME ftUserTime;
BOOL bRtn=GetProcessTimes(prodlg->pi.hProcess, // 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;
SYSTEMTIME stExitTime;
SYSTEMTIME stKernelTime;
SYSTEMTIME stUserTime;
FileTimeToSystemTime(&ftCreationTime,&stCreationTime);
FileTimeToSystemTime(&ftExitTime,&stExitTime);
FileTimeToSystemTime(&ftKernelTime,&stKernelTime);
FileTimeToSystemTime(&ftUserTime,&stUserTime);
m_dwCreateYear=stCreationTime.wYear;
m_dwCreateMonth=stCreationTime.wMonth;
m_dwCreateDay=stCreationTime.wDay;
m_dwCreateHour=stCreationTime.wHour;
m_dwCreateMinute=stCreationTime.wMinute;
m_dwCreateSecond=stCreationTime.wSecond;
m_dwExitYear=0;
m_dwExitMonth=0;
m_dwExitDay=0;
m_dwExitHour=0;
m_dwExitMinute=0;
m_dwExitSecond=0;
m_dwVersion=GetProcessVersion(m_dwID);
GetProcessWorkingSetSize(prodlg->pi.hProcess, // 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(prodlg->pi.hProcess, // 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
}
void CChildProcess::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CChildProcess::OnEndProcess()
{
// TODO: Add your control notification handler code here
DWORD dwExitCode;
CProcessDlg* prodlg=(CProcessDlg*)AfxGetApp()->GetMainWnd();
GetExitCodeProcess(prodlg->pi.hProcess, // handle to the process
&dwExitCode // address to receive termination status
);
if(TerminateProcess(prodlg->pi.hProcess,dwExitCode))
{
MessageBox("进程结束",NULL,0);
FILETIME ftCreationTime;
FILETIME ftExitTime;
FILETIME ftKernelTime;
FILETIME ftUserTime;
BOOL bRtn=GetProcessTimes(prodlg->pi.hProcess, // 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 stExitTime;
FileTimeToSystemTime(&ftExitTime,&stExitTime);
m_dwExitYear=stExitTime.wYear;
m_dwExitMonth=stExitTime.wMonth;
m_dwExitDay=stExitTime.wDay;
m_dwExitHour=stExitTime.wHour;
m_dwExitMinute=stExitTime.wMinute;
m_dwExitSecond=stExitTime.wSecond;
UpdateData(FALSE);
}
}
/////////////////////////////////////////
BOOL CChildProcess::SendCommand()
{
CWnd *pWnd=FindWindow(NULL,"ChildProcess");
//pWnd->SendMessage(wm_Receive,0,0);
UpdateData();
COPYDATASTRUCT buf;
if(m_strMessage.IsEmpty())
{
MessageBox("消息不能为空",NULL,0);
return FALSE;
}
char * s=new char[m_strMessage.GetLength()]; //m_Msg1为CString类型的变量
s=m_strMessage.GetBuffer(0);
buf.cbData=strlen(s)+1;
buf.lpData=s;
pWnd->SendMessage(WM_COPYDATA,0,(LPARAM)&buf); //传送大量数据要用WM_COPYDATA消息
return TRUE;
}
/////////////////////////////////////////////////
void CChildProcess::OnSendInfo()
{
// TODO: Add your control notification handler code here
if(SendCommand())
{
MessageBox("消息发送成功!","信息发送",0);
m_strMessage="";
UpdateData(FALSE);
}
else
{
MessageBox("信息发送不成功!","信息发送",0);
}
}
void CChildProcess::OnWritePipe()
{
// TODO: Add your control notification handler code here
//有名管道
HANDLE PipeHandle;
DWORD BytesWritten,TotalBytes;
BOOL bResult;
if (WaitNamedPipe("\\\\.\\PIPE\\child", NMPWAIT_USE_DEFAULT_WAIT) == 0)
{
MessageBoxPrintf("错误报告","WaitNamedPipe出现错误: %d\n",
GetLastError());
return;
}
// Create the named pipe file handle
if ((PipeHandle = CreateFile("\\\\.\\PIPE\\child",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL)) == INVALID_HANDLE_VALUE)
{
MessageBoxPrintf("错误保告","CreateFile出现错误: %d\n",
GetLastError());
return;
}
UpdateData();
TotalBytes=m_strMessage.GetLength();
char * s=new char[TotalBytes]; //m_Msg1为CString类型的变量
s=m_strMessage.GetBuffer(0);
while (1)
{
bResult=WriteFile(PipeHandle, s, TotalBytes, &BytesWritten,
NULL) ;
if(bResult==0)
{
MessageBoxPrintf("错误报告","WriteFile出现错误: %d\n",
GetLastError());
CloseHandle(PipeHandle);
return;
}
else
if(BytesWritten==TotalBytes)
{
MessageBox("写管道成功");
m_strMessage="";
UpdateData(FALSE);
CloseHandle(PipeHandle);
break;
}
continue;
}
//CloseHandle(PipeHandle);
//////无名管道////////////
}
void CChildProcess::OnWriteFileMap()
{
// TODO: Add your control notification handler code here
HANDLE hMapping;
LPSTR lpData;
hMapping=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x200,"MYSHARE");
if(hMapping==NULL)
{
MessageBox("CreateFileMapping() failed.");
return;
}
//将文件的视图映射到一个进程的地址空间上,返回LPVOID类型的内存指针
lpData=(LPSTR)MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0);
if(lpData==NULL)
{
MessageBox("MapViewOfFile() failed.");
return;
}
UpdateData();
if(m_strMessage.IsEmpty())
{
MessageBox("写内容不能为空");
return;
}
int iRtn;
iRtn=sprintf(lpData,m_strMessage);
//if(iRtn==sizeof(m_strMessage))
//{
MessageBox("写内存映象成功");
m_strMessage="";
UpdateData(FALSE);
//}
//else
//{
// MessageBox("写内存映象失败");
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -