dimanche 1 mars 2015

Trying to use CreateProcess to output a file, but hangs on output?



For part of a project, I have to be able to output a file through stdout using CreateProcess. However, it seems to be waiting for input after it outputs. It works when I just run source.exe by itself.


Main Program:



#include <winbase.h>

#define DELAY_A_WHILE() {volatile int j; for (j=0; j < 10000000; ++j) ; }
#define BUFSIZE 4096;

int main(int argc, char *argv[])
{
///VARS///

char cmdline1[MAX_PATH]; //Stores the command line for source.
char cmdline2[MAX_PATH]; //Stores the command line for sink.


STARTUPINFO StartupInfo; //Stores the startup information.
PROCESS_INFORMATION ProcInfoSource, ProcInfoFilter, ProcInfoSink; // Filled in by CreateProcess
// New Process Creation Flags:
DWORD CreateFlags = 0;
BOOL createStatus;

HANDLE sourceFilterRead, sourceFilterWrite, filterSinkRead, filterSinkWrite; //Handles for the two pipes.


//////////
//SECURITY_ATTRIBUTES PipeAttributes; //Pipe security attributes.
//PipeAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
//PipeAttributes.lpSecurityDescriptor = NULL; // ignore
//PipeAttributes.bInheritHandle = TRUE; // child can inherit
//////////
//////////SOURCE//
if (argc == 3)
{
strcpy(cmdline1, "Source.exe");
strcat(cmdline1, " ");
strcat(cmdline1, argv[1]); //Parse the first command line for source.exe.


strcpy(cmdline2, "sink.exe");
strcat(cmdline2, " ");
strcat(cmdline2, argv[2]); //Parse the second command line for sink.exe.

//if (!CreatePipe(&sourceFilterRead, &sourceFilterWrite, &PipeAttributes, 0)) { //Source | Filter
// printf("Error creating pipe: %d\n", GetLastError());
//}

///STARTUP///
GetStartupInfo(&StartupInfo);
/* StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
printf("%s\n", cmdline1);
printf("%s\n", cmdline2);
*/

createStatus = CreateProcess( NULL, cmdline1, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcInfoSource);
if(createStatus == 0)
{
printf("Source process failed.\n");
return(-1);
}

CloseHandle(ProcInfoSource.hProcess);
CloseHandle(ProcInfoSource.hThread);
printf("Source success.\n");
}
else
printf("Arg error\n");
return(0);
}


Source.exe:



#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#define DELAY_A_WHILE() {volatile int j; for(j=0; j<10000000; j++);}

int main(int argc, char *argv[])
{

int ch;
FILE *fin;
if(argc != 2 || fopen_s(&fin, argv[1], "rb"))
{
fprintf(stderr, "Usage: source inputfilename\n");
return(1);
}

//Read chars and copy to stdout.

while(1)
{
if ((ch = fgetc(fin)) == EOF)
break;
fputc(ch, stdout);

DELAY_A_WHILE();
}

fclose(fin);
return(0);
}



Aucun commentaire:

Enregistrer un commentaire