I can't seem to get this working and I have tried and tried. At the momenet it just prints out this...
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define BUFSIZE 2048
HANDLE stdout_write, stdout_read;
void create_pipe(void)
{
SECURITY_ATTRIBUTES attr;
attr.nLength = sizeof(SECURITY_ATTRIBUTES);
attr.bInheritHandle = TRUE;
attr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&stdout_read, &stdout_write, &attr, 0)) {
printf("CreatePipe() failed\n");
} else {
printf("CreatePipe() success\n");
}
if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0)) {
printf("SetHandleInformation() failed\n");
} else {
printf("SetHandleInformation() success\n");
}
}
void read_pipe_output(void)
{
char buf[BUFSIZE] = {0};
DWORD dwRead = 0;
BOOL bsuccess = 0;
for (;;) {
bsuccess = ReadFile(stdout_read, buf, BUFSIZE, &dwRead, NULL);
if( ! bsuccess || dwRead == 0 ) break;
}
printf("buf: %s\n", &buf[0]);
}
int main(int argc, char *argv[])
{
char *fuck = "C:\\Windows\\System32\\cmd.exe";
char *cmd = "C:\\Windows\\System32\\cmd.exe /c dir";
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0x0, sizeof(si));
memset(&pi, 0x0, sizeof(pi));
si.cb = sizeof(STARTUPINFO);
si.wShowWindow = FALSE;
si.hStdOutput = stdout_write;
si.hStdError = stdout_write;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
create_pipe();
if (!CreateProcessA(fuck, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
printf("error\n");
} else {
CloseHandle(stdout_write);
}
read_pipe_output();
CloseHandle(stdout_read);
printf("completed\n");
return 0;
}
I want to print the output of "ls" which works fine but I want to use a pipe so I can do some stuff with the output data.
Any help appreciated!