15,467
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <iostream>
#include <stdio.h>
void main()
{
SECURITY_ATTRIBUTES sa;
HANDLE hRead, hWrite, hRead2, hWrite2;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0)) {
MessageBox(NULL, "Error On CreatePipe()", "",MB_OK);
return;
}
if (!CreatePipe(&hRead2,&hWrite2,&sa,0)) {
MessageBox(NULL, "Error On CreatePipe()", "",MB_OK);
return;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.hStdInput = hRead2;//GetStdHandle(STD_INPUT_HANDLE);
//si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL,"testData.exe"
,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)) {
MessageBox(NULL, "Error on CreateProcess()", "", MB_OK);
return;
}
CloseHandle(hWrite);
char buffer[4096] = {0};
DWORD bytesRead, bytesRead2;
char str1[1000]= "1234\n";
WriteFile(hWrite2, str1, 6,&bytesRead2,NULL);
WriteFile(hWrite2, str1, 6,&bytesRead2,NULL); //为什么这一句没有write进管道
while (true) {
memset(buffer, 0, sizeof(buffer));
if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
{
MessageBox(NULL, "Error on ReadFile()", "", MB_OK);
break;
}
if (strlen(buffer) > 0)
{
printf("HaHa:%s",buffer);
//Sleep(4000);
char str1[1000]= "12345\n";
WriteFile(hWrite2, str1, 7,&bytesRead2,NULL);//为什么这一句没有write进管道
}
}
}
#include <stdio.h>
#include <Windows.h>
int main()
{
char str[100]="1";
int count = 0;
while(true)
{
scanf("%s", str);
Sleep(1000);
printf("testData %d:%s\n",count, str);
fflush(stdout);
count++;
}
Sleep(4000);
return 0;
}
if(!WriteFile(hWrite2, str1, 6,&bytesRead2,NULL))
MessageBox(NULL, "Error on WriteFile()", "", MB_OK);