I am trying to create a function that will spawn an instance of a program, and then pass some data to its STDIN, and then read the process output using C ++. I looked at the MSDN example located here , which is rather confusing for me, and when I try to use this example, I get some nasty error codes and it will not work.
HANDLE hWriteOUT, hReadOUT, hWriteIN, hReadIN;
SECURITY_ATTRIBUTES saPipe = {0};
PROCESS_INFORMATION procInfo = {0};
STARTUPINFO procSi;
DWORD dwWritten, dwRead;
char buf[512];
saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
saPipe.bInheritHandle = TRUE;
saPipe.lpSecurityDescriptor= NULL;
CreatePipe(&hReadOUT, &hWriteOUT, &saPipe, 0);
SetHandleInformation(hReadOUT, HANDLE_FLAG_INHERIT, 0);
CreatePipe(&hReadIN, &hWriteIN, &saPipe, 0);
SetHandleInformation(hReadIN, HANDLE_FLAG_INHERIT, 0);
ZeroMemory(&procSi, sizeof(STARTUPINFO));
procSi.cb = sizeof(STARTUPINFO);
procSi.hStdError = hWriteOUT;
procSi.hStdOutput = hWriteOUT;
procSi.hStdInput = hReadIN;
procSi.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(NULL, "cmd", NULL, NULL, TRUE, 0, NULL, NULL, &procSi, &procInfo);
WriteFile(hWriteIN, "notepad", sizeof("notepad"), &dwWritten, NULL);
cout << GetLastError();
ReadFile(hReadOUT, buf, 512, &dwRead, NULL);
cout << buf;
CloseHandle(hWriteIN);
"" cmd.exe, . , , . , ReadFile() , , , , (, ) , CMD. , , , ! (CMD "Microsoft Windows...\n ... \n C:\Users\Foo > ...\n", `ReadFile() )