尝试使用CreateProcess输出文件,但挂起输出?

时间:2015-03-02 04:04:57

标签: c stdout createprocess

对于项目的一部分,我必须能够使用CreateProcess通过stdout输出文件。但是,它输出后似乎在等待输入。它只在我自己运行source.exe时有效。

主程序:

#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);
}
编辑:我找到了解决方案。如果我在启动进程后立即使用WaitForSingleObject(),它可以正常工作。没有它,不确定是否有办法做到这一点。分配的目标是让一堆进程同时工作而不等待彼此,但我还没有找到另一种方法来完成它而不等待source.exe完成。

0 个答案:

没有答案