VC如何利用匿名管道实现进程间通信?
1个回答
2013-05-09
展开全部
void CParentProcessDlg::OnOK()
{
SECURITY_ATTRIBUTES SecurityPipe;
SecurityPipe.bInheritHandle = TRUE;
SecurityPipe.lpSecurityDescriptor = NULL;
SecurityPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
if (CreatePipe(&m_hRead,&m_hWrite,&SecurityPipe,0))
{
STARTUPINFO strinfo;
memset(&strinfo,0,sizeof(STARTUPINFO));
strinfo.cb = sizeof(STARTUPINFO); strinfo.hStdInput = m_hRead;
strinfo.dwFlags = STARTF_USESTDHANDLES;
strinfo.hStdOutput = m_hWrite; PROCESS_INFORMATION procInfo;
memset(&procInfo,0,sizeof(PROCESS_INFORMATION)); CreateProcess(NULL,"ChildProcess.exe",&SecurityPipe,&SecurityPipe,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&strinfo,&procInfo);
char data[]="明日科技";
unsigned long len;
if (! WriteFile(m_hWrite,data,8,&len,NULL))
{
MessageBox("数据写入失败");
}
}
else
MessageBox("匿名管道创建失败");
}
void CChildProcessDlg::OnButtonread()
{
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
if (hInput != INVALID_HANDLE_VALUE )
{
char data[10];
memset(data,0,10);
unsigned long len;
int pos = ReadFile(hInput,data,10,&len,NULL);
m_Data = data;
UpdateData(FALSE);
CloseHandle(hInput);
}
}
{
SECURITY_ATTRIBUTES SecurityPipe;
SecurityPipe.bInheritHandle = TRUE;
SecurityPipe.lpSecurityDescriptor = NULL;
SecurityPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
if (CreatePipe(&m_hRead,&m_hWrite,&SecurityPipe,0))
{
STARTUPINFO strinfo;
memset(&strinfo,0,sizeof(STARTUPINFO));
strinfo.cb = sizeof(STARTUPINFO); strinfo.hStdInput = m_hRead;
strinfo.dwFlags = STARTF_USESTDHANDLES;
strinfo.hStdOutput = m_hWrite; PROCESS_INFORMATION procInfo;
memset(&procInfo,0,sizeof(PROCESS_INFORMATION)); CreateProcess(NULL,"ChildProcess.exe",&SecurityPipe,&SecurityPipe,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&strinfo,&procInfo);
char data[]="明日科技";
unsigned long len;
if (! WriteFile(m_hWrite,data,8,&len,NULL))
{
MessageBox("数据写入失败");
}
}
else
MessageBox("匿名管道创建失败");
}
void CChildProcessDlg::OnButtonread()
{
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
if (hInput != INVALID_HANDLE_VALUE )
{
char data[10];
memset(data,0,10);
unsigned long len;
int pos = ReadFile(hInput,data,10,&len,NULL);
m_Data = data;
UpdateData(FALSE);
CloseHandle(hInput);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询