Problem with AllocConsole and child processes Windows 7 - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: Problem with AllocConsole and child processes Windows 7

  1. #16
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Rich C View Post
    Thanks Paul. Good catch.

    Rich
    It's one of those things that has happened to me, albeit with a different struct (I think it was the OPENFILENAME struct). I always inspect the struct that Windows API wants to use, just to see if a "size of struct" member is available. I set it, even if in reality, it wouldn't / shouldn't make a difference.

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jul 2013
    Posts
    10

    Re: Problem with AllocConsole and child processes Windows 7

    Paul,

    I did the following in the function and it did not work. Note: I set si.cb:
    if(AllocConsole())
    {
    outp = GetStdHandle(STD_OUTPUT_HANDLE);
    coord.X = 80;
    coord.Y = 500;
    SetConsoleScreenBufferSize(outp,coord);
    SetConsoleTitle("Text Output");
    }

    si.cb = sizeof(si);
    GetStartupInfo(&si);

    ret = CreateProcess(NULL,command,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
    if (ret != TRUE)
    return(-1);

    Rich

  3. #18
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Rich C View Post
    Paul,

    I did the following in the function and it did not work.
    Define what you mean by "did not work".

    Second, why are you not calling GetLastError() if CreateProcess fails?

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Return value
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Jul 2013
    Posts
    10

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Paul McKenzie View Post
    Define what you mean by "did not work".

    Second, why are you not calling GetLastError() if CreateProcess fails?

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Regards,

    Paul McKenzie
    Sorry, I was assuming you had looked at my original post. This was simply taking Igor's code and using GetStartupInfo. The problem is that when executing the code from the windows explorer, the console window appears but nothing is written to it.
    Here is the complete function. You must supply a driver program to execute a child process that writes to standard output.

    int execute_command(char *command)
    {
    HANDLE outp;
    COORD coord;
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi = {0};
    DWORD exitcode;
    BOOL ret;

    if(AllocConsole())
    {
    outp = GetStdHandle(STD_OUTPUT_HANDLE);
    coord.X = 80;
    coord.Y = 500;
    SetConsoleScreenBufferSize(outp,coord);
    SetConsoleTitle("Text Output");
    }
    si.cb = sizeof(si);
    GetStartupInfo(&si);

    ret = CreateProcess(NULL,command,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi);
    if (ret != TRUE)
    return(-1);

    exitcode = STILL_ACTIVE;
    if (pi.hProcess)
    {
    MSG msg;
    ret = WAIT_TIMEOUT;
    while (ret == WAIT_TIMEOUT || exitcode == STILL_ACTIVE)
    {
    ret = WaitForSingleObject(pi.hProcess,10);
    if (!GetExitCodeProcess(pi.hProcess,&exitcode))
    MessageBox(NULL, "GetExitCodeProcess Failure", NULL, MB_OK);
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    DispatchMessage(&msg);
    }
    }
    if (pi.hProcess)
    CloseHandle(pi.hProcess);
    if (pi.hThread)
    CloseHandle(pi.hThread);

    return(exitcode);
    }

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,023

    Re: Problem with AllocConsole and child processes Windows 7

    Is the problem with CreateProcess? Is the console being created OK but CreateProcess is failing? When you use exeute_command in your program, do you test the return value for -1 and display an error message box or something as you don't show how you are calling execute_command? What commands are you passing to execute_command? What is the value of GetLastError() if the CreateProcess function fails?

    For every command you want executed, you are creating and destroying a new process. This takes time. Why not just create a new process once with a console window and have it execute cmd to get a command prompt window?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #21
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,016

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Rich C View Post
    Code:
         if(AllocConsole())
    	{
    		outp = GetStdHandle(STD_OUTPUT_HANDLE);
    		coord.X = 80;
    		coord.Y = 500;
    		SetConsoleScreenBufferSize(outp,coord);
    		SetConsoleTitle("Text Output");
    	}
    
         si.cb = sizeof(si);
         GetStartupInfo(&si);
    
         ret = CreateProcess(NULL,command,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
         if (ret != TRUE)
    	return(-1);
    Please, use Code tags while posting code snippets!
    And never compare BOOL values returned by Windows APIs with TRUE! TRUE is #defined as 1 while a lot of APIs may return any other non-zero value as a succesful result.
    So the correct comparison should be something like
    Code:
        if (!ret)
    or
    Code:
        if (ret == 0)
    And as others mentioned you have to call GetLastError to obtain the reason of the failure!
    Victor Nijegorodov

  7. #22
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Rich C View Post
    Sorry, I was assuming you had looked at my original post. This was simply taking Igor's code and using GetStartupInfo.
    First, use code tags when posting code:

    [code]
    Your code goes here
    [/code]

    That is how you use code tags.

    Second, you need to help us out more. All of those API functions you're assuming work OK may not be OK, as they return values. Those values could indicate failure, but your original function (and the one you took from Igor's example) doesn't check for these errors. You should rewrite the code so that you are checking all return codes, and if the code is failure, then call GetLastError() (if the documentation for the function states this). This way, you and others helping you have a better picture of what is happening. Not only that, this is the way you're supposed to write any software that could be run on any computer, whether it's yours or a customer's machine.

    The bottom line is that you should never assume that Windows functions just "work". I remember having to rewrite a bunch of functions to check for the return code from a bunch of GDI functions -- without the check for the return code, the functions were writing totally black pages when printing, causing the customer to waste vast amounts of ink because of a failed function (that was never checked for failure)!

    Regards,

    Paul McKenzie

  8. #23
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,523

    Re: Problem with AllocConsole and child processes Windows 7

    Quote Originally Posted by Rich C View Post
    I assumed when I called GetStartupInfo to fill in the STARTUPINFO structure, the first member of the STARTUPINFO structure would have been set
    In addition to what Igor and Paul stated, I would like to suggest NOT to pass uninitialized data (garbage) to any Win API function that might expect NULL or valid pointer for some fields.
    It costs you next to nothing to set everything to 0 before filling in relevant data.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center