Тема: Підкажіть чому ОС не дає писати у "пайп"
Читати можу писати ні.
int main()
{
HANDLE rIn, rOut, wIn, wOut;
SECURITY_ATTRIBUTES sa = { 0 };
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (CreatePipe(&rIn, &wIn, &sa, 0) == 0)
{
printf("%d\n", GetLastError());
}
if (CreatePipe(&rOut, &wOut, &sa, 0) == 0)
{
printf("%d\n", GetLastError());
}
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = wIn;
si.hStdOutput = wOut;
si.hStdError = wOut;
wchar_t cmdline[1024] = L"cmd.exe";
if (CreateProcessW(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) == 0)
{
printf("CreateProcess err:%d\n", GetLastError());
}
else
{
DWORD r = 0;
char *buf = new char[4096];
DWORD bwait = 0;
for (;;)
{
if (PeekNamedPipe(rOut, 0, 0, 0, &bwait, 0) == FALSE)
{
printf("PeekNamedPipe err: %d\n", GetLastError());
}
else
{
if (bwait > 0)
{
bwait = 0;
memset(buf, 0, 4096);
ReadFile(rOut, buf, 4096, &r, 0);
std::cout << buf;
//printf("%s", buf);
}
else
{
memset(buf, 0, 4096);
std::cin >> buf;
DWORD w = 0;
if (WriteFile(rIn, buf, strlen(buf), &w, 0) == FALSE)
{
printf("wf err: %d\n", GetLastError());
}
//printf("w: %d", w);
if (strcmp(buf, "exit") == 0)
{
break;
}
}
}
}
TerminateProcess(pi.hProcess, 0);
CloseHandle(rIn);
CloseHandle(wIn);
CloseHandle(rOut);
CloseHandle(wOut);
}
_getch();
return 0;
}
Де я помилився?