参考网址:
我的测试代码:
Pipe_Server_VC6_Console.exe :
1 #include2 3 #include 4 using namespace std; 5 6 int main() 7 { 8 //定义四个句炳保留两个管道的信息 9 HANDLE hReadPipe1, hWritePipe1, hReadPipe2, hWritePipe2;10 SECURITY_ATTRIBUTES sat;11 STARTUPINFO startupinfo;12 PROCESS_INFORMATION pinfo;13 BYTE buffer[1024];14 DWORD byteRead, byteWrite;15 16 string rString;17 string m_Host= "Pipe_Client_VC6_Console.exe";18 19 sat.nLength=sizeof(SECURITY_ATTRIBUTES);20 sat.bInheritHandle=true;21 sat.lpSecurityDescriptor=NULL;22 23 //创建管道,它用来做子进程的输出24 if(!CreatePipe(&hReadPipe1, &hWritePipe1, &sat, NULL))25 {26 cout<<("Create Pipe Error!")<
Pipe_Client_VC6_Console.exe :
1 #include2 3 #include 4 using namespace std; 5 6 int main(int argc, char* argv[]) 7 { 8 HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE); 9 HANDLE hWrite = GetStdHandle(STD_OUTPUT_HANDLE);10 11 char s[] = "Hello, I am child process\n";12 DWORD dwWrite;13 14 if (!WriteFile(hWrite, s, strlen(s) + 1, &dwWrite, NULL))15 {16 cout << "Write to pipe failed!" << endl;17 return -1;18 }19 20 char buf[100];21 DWORD dwRead;22 if(!ReadFile(hRead, buf, 100, &dwRead, NULL))23 {24 cout << "Read from pipe failed!" << endl;25 return -1;26 }27 28 return 0;29 }
C