Thread: Help with CreateProcess()

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    37

    Question Help with CreateProcess()

    Hi,

    I have a problem using CreateProcess().

    Let me show you the code first:

    Code:
    if(!CreateProcess(NULL,   // No module name (use command line)
       "POI.exe < data1.in > data1.out",        // Command line
       NULL,           // Process handle not inheritable
       NULL,           // Thread handle not inheritable
       FALSE,          // Set handle inheritance to FALSE
       0,              // No creation flags
       NULL,           // Use parent's environment block
       NULL,           // Use parent's starting directory
       &si,            // Pointer to STARTUPINFO structure
       &pi)           // Pointer to PROCESS_INFORMATION structure
    ){
       printf( "CreateProcess failed (%d).\n", GetLastError() );
       scanf("%d", &n);
       return 0;
    }
    (Obviously I copied the code from MSDN and only modified a little :P)

    I executed it, and it was sucessfully running POI.exe. However, the input and output are not redirected to data1.in and data1.out. Why is this happening? What's the solution to it?

    Thanks in advance.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I/O redirection is handled by the command processor (cmd.exe). You can use system() instead, or take care of the I/O redirection yourself.
    Creating a Child Process with Redirected Input and Output (Windows)

    gg

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    The output redirection should be specified in the STARTUPINFO struct; first open a handle to the in and out files and then use those handles in the struct:

    Code:
    STARTUPINFO si;
    HANDLE hin,hout;
    
    hin=CreateFile(...);//for reading
    hout=CreateFile(...);//for writing
    
    memset(&si,0,sizeof(si));
    si.cb=sizeof(si);
    si.dwFlags=STARTF_USESTDHANDLES;
    si.hStdInput=hin;
    si.hStdOutput=hout;
    
    //remember to close the handles
    If you like you can also redirect the std error output assigning to the hStdError struct's member a handle to an errors file.

    Hope that helps
    Niara

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Oops I've seen it now: I've explained the same as the the Codeplug's link. Sorry.

    Niara

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    Thanks for your help. But now I'm having trouble with this code:

    Code:
       STARTUPINFO si;
       PROCESS_INFORMATION pi;
    
       HANDLE hin = CreateFile("data1.in", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
       if(hin == INVALID_HANDLE_VALUE){
          printf("in error %d\n", GetLastError());
       }
       HANDLE hout = CreateFile("data1.out", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
       if(hout == INVALID_HANDLE_VALUE){
          printf("out error %d\n", GetLastError());
       }
    
       ZeroMemory(&si, sizeof(si));
       si.cb = sizeof(si);
       si.dwFlags = STARTF_USESTDHANDLES;
       si.hStdInput = hin;
       si.hStdOutput = hout;
       ZeroMemory(&pi, sizeof(pi));
    
       if(!CreateProcess(NULL,   // No module name (use command line)
          "POI",        // Command line
          NULL,           // Process handle not inheritable
          NULL,           // Thread handle not inheritable
          FALSE,          // Set handle inheritance to FALSE
          0,              // No creation flags
          NULL,           // Use parent's environment block
          NULL,           // Use parent's starting directory
          &si,            // Pointer to STARTUPINFO structure
          &pi)           // Pointer to PROCESS_INFORMATION structure
       ){
          printf( "CreateProcess failed (%d).\n", GetLastError() );
          scanf("%d", &n);
          return 0;
       }
    It seems that the input is not working, but hin != INVALID_HANDLE_VALUE, and somehow the POI.exe just seems not to read from data1.in at all and the whole program is stuck. Any ideas?

    Thanks.
    Last edited by jasperleeabc; 09-26-2009 at 08:02 AM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Call SetHandleInformation() on hin and hout - like in the article I posted.
    The 5th parameter to CreateProcess() should be TRUE.

    gg

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    37
    This is still not working?

    Code:
       STARTUPINFO si;
       PROCESS_INFORMATION pi;
    
       HANDLE hin = CreateFile("data1.in", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
       if (!SetHandleInformation(hin, HANDLE_FLAG_INHERIT, 0)){
          printf("set in handle error %d", GetLastError());
          scanf("%d", &n);
          return 0;
       }
       if(hin == INVALID_HANDLE_VALUE){
          printf("in error %d\n", GetLastError());
       }
       HANDLE hout = CreateFile("data1.out", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
       if (!SetHandleInformation(hout, HANDLE_FLAG_INHERIT, 0)){
          printf("set out handle error %d", GetLastError());
          scanf("%d", &n);
          return 0;
       }
       if(hout == INVALID_HANDLE_VALUE){
          printf("out error %d\n", GetLastError());
       }
    
       ZeroMemory(&si, sizeof(si));
       si.cb = sizeof(si);
       si.dwFlags = STARTF_USESTDHANDLES;
       si.hStdInput = hin;
       si.hStdOutput = hout;
       ZeroMemory(&pi, sizeof(pi));
    
       if(!CreateProcess(NULL,   // No module name (use command line)
          "POI",        // Command line
          NULL,           // Process handle not inheritable
          NULL,           // Thread handle not inheritable
          TRUE,          // Set handle inheritance to TRUE
          0,              // No creation flags
          NULL,           // Use parent's environment block
          NULL,           // Use parent's starting directory
          &si,            // Pointer to STARTUPINFO structure
          &pi)           // Pointer to PROCESS_INFORMATION structure
       ){
          printf( "CreateProcess failed (%d).\n", GetLastError() );
          scanf("%d", &n);
          return 0;
       }
    I get error no.5 when setting the hin handle.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Error 5 is access denied. Probably that means data1.in doesn't exist (where the computer thinks it should). It should be in the current working directory, which IIRC is not the directory where your executable is (i.e., not Debug or Release but the one above that).

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    37
    data1.in is in the directory my executable is in, how should I do it?

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Check for INVALID_HANDLE_VALUE after CreateFile. Then call SetHandleInformation on both hin and hout.

    Then you should wait for the process the exit.

    gg

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jasperleeabc View Post
    data1.in is in the directory my executable is in, how should I do it?
    Perhaps I'm hallucinating again, but I believe that is not the right place for it. It shoudl be up one directory level.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    37
    I don't have any errors printed now, but it's still not reading from data1.in...

    Code:
    Code:
       STARTUPINFO si;
       PROCESS_INFORMATION pi;
    
       HANDLE hin = CreateFile("E:\\Personal Data\\Jasper's Stuff\\C\\IOI 2009\\Day 1\\data1.in", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
       if(hin == INVALID_HANDLE_VALUE){
          printf("in error %d\n", GetLastError());
       }
       if (!SetHandleInformation(hin, HANDLE_FLAG_INHERIT, 0)){
          printf("set in handle error %d", GetLastError());
          scanf("%d", &n);
          return 0;
       }
       HANDLE hout = CreateFile("E:\\Personal Data\\Jasper's Stuff\\C\\IOI 2009\\Day 1\\data1.out", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
       if(hout == INVALID_HANDLE_VALUE){
          printf("out error %d\n", GetLastError());
       }
       if (!SetHandleInformation(hout, HANDLE_FLAG_INHERIT, 0)){
          printf("set out handle error %d", GetLastError());
          scanf("%d", &n);
          return 0;
       }
       ZeroMemory(&si, sizeof(si));
       si.cb = sizeof(si);
       si.dwFlags = STARTF_USESTDHANDLES;
       si.hStdInput = hin;
       si.hStdOutput = hout;
       ZeroMemory(&pi, sizeof(pi));
    
       if(!CreateProcess(NULL,   // No module name (use command line)
          "POI",        // Command line
          NULL,           // Process handle not inheritable
          NULL,           // Thread handle not inheritable
          TRUE,          // Set handle inheritance to TRUE
          0,              // No creation flags
          NULL,           // Use parent's environment block
          NULL,           // Use parent's starting directory
          &si,            // Pointer to STARTUPINFO structure
          &pi)           // Pointer to PROCESS_INFORMATION structure
       ){
          printf( "CreateProcess failed (%d).\n", GetLastError() );
          scanf("%d", &n);
          return 0;
       }
    My program is like now stuck and can't do anything.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Call SetHandleInformation() on hin and hout - like in the article I posted.
    Well, not *exactly* like in the article. You want hin and hout to be inheritable, but you're clearing HANDLE_FLAG_INHERIT instead of setting it.

    gg

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    37
    Problem solved, many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateProcess() + Command Line
    By IndioDoido in forum Windows Programming
    Replies: 20
    Last Post: 11-14-2008, 07:35 PM
  2. CreateProcess with arguments
    By Niara in forum Windows Programming
    Replies: 14
    Last Post: 09-08-2007, 05:41 AM
  3. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 01:07 AM
  4. CreateProcess
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 05-12-2002, 06:45 AM
  5. CreateProcess()
    By Newfie in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2002, 07:31 AM