利用管道进程间通信的问题 本帖最后由 sound_of_silence 于 2009-06-20 21:19:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建议你参阅一下微软一站式技术框架所提供的pipe的例子:CppNamedPipeClientCppNamedPipeServerhttp://cfx.codeplex.com 谢谢 All-In-One Code Framework Project Group http://cfx.codeplex.com 匿名管道数据是单向的,如果要用匿名管道的话,需要用2个才能完成双向通信。或者用NamedPipe。int main(){ //定义四个句炳保留两个管道的信息 HANDLE hReadPipe1, hWritePipe1, hReadPipe2, hWritePipe2; SECURITY_ATTRIBUTES sat; STARTUPINFO startupinfo; PROCESS_INFORMATION pinfo; BYTE buffer[1024]; DWORD byteRead, byteWrite; string rString; //string m_Host="c:\\windows\\system32\\cmd.exe "; string m_Host= "Client.exe"; sat.nLength=sizeof(SECURITY_ATTRIBUTES); sat.bInheritHandle=true; sat.lpSecurityDescriptor=NULL; //创建管道,它用来做子进程的输出 if(!CreatePipe(&hReadPipe1, &hWritePipe1, &sat, NULL)) { cout<<("Create Pipe Error!")<<endl; return 1; } //创建管道,它用来做子进程的输入 if(!CreatePipe(&hReadPipe2, &hWritePipe2, &sat, NULL)) { cout<<"Create Pipe Error!"<<endl; return 1; } startupinfo.cb=sizeof(STARTUPINFO); //用GetStartupInfo获得当前进程的参数,否则STARTUPINFO参数太多,会让人抓狂 GetStartupInfo(&startupinfo); startupinfo.hStdInput = hReadPipe2; startupinfo.hStdError=hWritePipe1; startupinfo.hStdOutput=hWritePipe1; //要有STARTF_USESTDHANDLES,否则hStdInput, hStdOutput, hStdError无效 startupinfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; startupinfo.wShowWindow=SW_HIDE; if(!CreateProcess(NULL, (char*)m_Host.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &startupinfo, &pinfo)) { cout<<("create process error!")<<endl; return 1; } RtlZeroMemory(buffer,1024); //进程的输出重定向到hWritePipe1,所以从hReadPipe1读取 if(ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL)==NULL) return -1; cout<<buffer; string csWrite = "ping www.sina.com.cn\r\n"; //进程的输入重定向到hReadPipe2,所以从hWritePipe2写入 if (!WriteFile(hWritePipe2, (LPCVOID)csWrite.c_str(), csWrite.length() + 1, &byteWrite, NULL)) return 1; RtlZeroMemory(buffer,1024); //进程的输出重定向到hWritePipe1,所以从hReadPipe1读取 if(ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL)==NULL) return -1; cout<<buffer; Sleep(20); CloseHandle(hReadPipe1); CloseHandle(hReadPipe2); CloseHandle(hWritePipe1); CloseHandle(hWritePipe2); return 0;} 这个正则表达式怎么写 有谁了解【青牛软件】啊,这个公司怎么样?进着有分 ado入门问题,简单 求助:将子对话框做成lib 请问如何较高效率地从MDB数据库,一次读取将近十万条记录?? 读最后一条记录引起程序中断的问题,在线等,请指教! 新手,程序实例是精通还是泛通? 不知那位大哥可以为我提供以下资料: 为什么同一个 CreateFile 语句,在2000下能成功运行,98下就返回错误的句柄?? Jedi(杰迪) 清关注 窗体间数据传递问题。 如何判断WebBrowser控件打开一个页面是成功还是失败。
CppNamedPipeServerhttp://cfx.codeplex.com 谢谢
All-In-One Code Framework Project Group
http://cfx.codeplex.com
int main()
{
//定义四个句炳保留两个管道的信息
HANDLE hReadPipe1, hWritePipe1, hReadPipe2, hWritePipe2;
SECURITY_ATTRIBUTES sat;
STARTUPINFO startupinfo;
PROCESS_INFORMATION pinfo;
BYTE buffer[1024];
DWORD byteRead, byteWrite;
string rString;
//string m_Host="c:\\windows\\system32\\cmd.exe ";
string m_Host= "Client.exe";
sat.nLength=sizeof(SECURITY_ATTRIBUTES);
sat.bInheritHandle=true;
sat.lpSecurityDescriptor=NULL;
//创建管道,它用来做子进程的输出
if(!CreatePipe(&hReadPipe1, &hWritePipe1, &sat, NULL))
{
cout<<("Create Pipe Error!")<<endl;
return 1;
}
//创建管道,它用来做子进程的输入
if(!CreatePipe(&hReadPipe2, &hWritePipe2, &sat, NULL))
{
cout<<"Create Pipe Error!"<<endl;
return 1;
}
startupinfo.cb=sizeof(STARTUPINFO);
//用GetStartupInfo获得当前进程的参数,否则STARTUPINFO参数太多,会让人抓狂
GetStartupInfo(&startupinfo);
startupinfo.hStdInput = hReadPipe2;
startupinfo.hStdError=hWritePipe1;
startupinfo.hStdOutput=hWritePipe1;
//要有STARTF_USESTDHANDLES,否则hStdInput, hStdOutput, hStdError无效
startupinfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupinfo.wShowWindow=SW_HIDE;
if(!CreateProcess(NULL, (char*)m_Host.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &startupinfo, &pinfo))
{
cout<<("create process error!")<<endl;
return 1;
} RtlZeroMemory(buffer,1024);
//进程的输出重定向到hWritePipe1,所以从hReadPipe1读取
if(ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL)==NULL)
return -1;
cout<<buffer; string csWrite = "ping www.sina.com.cn\r\n";
//进程的输入重定向到hReadPipe2,所以从hWritePipe2写入
if (!WriteFile(hWritePipe2, (LPCVOID)csWrite.c_str(), csWrite.length() + 1, &byteWrite, NULL))
return 1;
RtlZeroMemory(buffer,1024);
//进程的输出重定向到hWritePipe1,所以从hReadPipe1读取
if(ReadFile(hReadPipe1,buffer,1023,&byteRead,NULL)==NULL)
return -1;
cout<<buffer;
Sleep(20); CloseHandle(hReadPipe1);
CloseHandle(hReadPipe2);
CloseHandle(hWritePipe1);
CloseHandle(hWritePipe2);
return 0;}