基本信息
源码名称:VC6.0实现进程控制与通信
源码大小:6.98M
文件格式:.rar
开发语言:C/C++
更新时间:2019-12-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

1、          在进程A中输入一些字符,点“利用SendMessage发送消息”按钮可将消息发到进程B。

2、          在进程A中输入一些字符,点“写数据到内存映像文件”按钮,然后在进程B中点“从内存映像文件读数据” 按钮可收到消息。



void CProcessADlg::OnSendmessage() 
{
// TODO: Add your control notification handler code here
CString str="ProcessB"; 
CWnd *pWnd=CWnd::FindWindow(NULL,str); 
if(pWnd) 

COPYDATASTRUCT buf; 
GetDlgItem(IDC_EDIT3)->GetWindowText(m_Msg1);
char * s=new char[m_Msg1.GetLength()]; //m_Msg1为CString类型的变量
s=m_Msg1.GetBuffer(0); 
buf.cbData=strlen(s) 1; 
buf.lpData=s; 
pWnd->SendMessage(WM_COPYDATA,0,(LPARAM)&buf); //传送大量数据要用WM_COPYDATA消息


}

void CProcessADlg::OnWritetopipe() 
{
// TODO: Add your control notification handler code here
  
}

void CProcessADlg::OnWrite() 
{
// 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)   
{   
AfxMessageBox("CreateFileMapping() failed.");
return;
}
//将文件的视图映射到一个进程的地址空间上,返回LPVOID类型的内存指针
lpData=(LPSTR)MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0);   
if(lpData==NULL)   
{   
AfxMessageBox("MapViewOfFile() failed.");
return;
}
GetDlgItem(IDC_EDIT3)->GetWindowText(m_Msg1);
//给这段映像内存写数据
sprintf(lpData,m_Msg1);   
//释放映像内存
UnmapViewOfFile(lpData);   
}

void CProcessADlg::OnCreateB() 
{
// TODO: Add your control notification handler code here
PROCESS_INFORMATION pi;
STARTUPINFO si;
//初始化变量
memset(&si,0,sizeof(si));
si.cb=sizeof(si);
si.wShowWindow=SW_SHOW;
si.dwFlags=STARTF_USESHOWWINDOW;

//打开ProcessB
BOOL fRet=CreateProcess(NULL,
"G:\\OS\\ProcessB\\Debug\\ProcessB.exe",
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);

if(!fRet)
{//创建失败,显示错误信息
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM | 
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
AfxMessageBox( (LPCTSTR)lpMsgBuf);
LocalFree( lpMsgBuf );

}
else
{
AfxMessageBox("CreateProcess成功");
m_hPro=pi.hProcess;
}
}

void CProcessADlg::OnEndB() 
{
// TODO: Add your control notification handler code here

//判断进程句柄是否合法
if(m_hPro)
{
//根据句柄,终止刚才打开的ProcessB程序
if(!TerminateProcess(m_hPro,0))
{
//终止出现错误,显示错误信息
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM | 
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
AfxMessageBox( (LPCTSTR)lpMsgBuf);
LocalFree( lpMsgBuf );
}
else
{
AfxMessageBox("退出成功");
}
m_hPro=NULL;
}
else
{
AfxMessageBox("m_hPro为空");
}
}

void CProcessADlg::OnExit() 
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}

void CProcessADlg::OnShuru() 
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
//CString str;
   // GetDlgItem(IDC_EDIT3)->GetWindowText(str);
}