Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
Rich C
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
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
Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
Rich C
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
Quote:
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
Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
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);
}
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?
Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
Rich C
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 likeor And as others mentioned you have to call GetLastError to obtain the reason of the failure!
Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
Rich C
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
Re: Problem with AllocConsole and child processes Windows 7
Quote:
Originally Posted by
Rich C
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.