Re: Executable return values
Try shellexecute or shellexecuteex, you should get a return value.
Re: Executable return values
Thanks, but as far as I can tell ShellExecute returns right away and doesn't return the value returned by my launched program's main function.
I need my program to wait until the launched program stops running.
Is there a way to do this?
Re: Executable return values
under Unix, there is the system() call that does what you want. I don't know right know if there is a similar call under Windows.
Re: Executable return values
Here's a wrapper function that should be able to do what you want:
Code:
BOOL ExecuteAndWaitForCompletion ( LPCTSTR pszCmd)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory ( &si, sizeof ( STARTUPINFO));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
bRes = CreateProcess ( NULL,
pszCmd
NULL,
NULL,
TRUE,
NORMAL_PRIORITY_CLASS,
GetEnvironmentStrings (),
NULL,
&si,
&pi
);
WaitForSingleObject ( pi.hProcess, INFINITE);
CloseHandle( pi.hProcess);
CloseHandle( pi.hThread);
return ( bRes);
}
Re: Executable return values
Well...the first question would be....what operating system are you using? In case of Windows...take a look at the following FAQs:
To get the exit code of the process....you can use 'GetExitCodeProcess()'...
Re: Executable return values
Quote:
Originally Posted by Andreas Masur
FYI:
It's strange that the above list of methods seems to exclude the most obvious.
It's excluding the system() function, which is the only C/C++ method defined in the C/C++ standard.
Although _exec and _spawn are in the C run-time library for the VC++ compiler, these two functions are not part of the C/C++ standard. IMHO, the FAQ might lead a newbie to believe that it is part of the C/C++ standard.
I recommend updating the above FAQ to add the system() function, and to clarify that _exec and _spawn are not part of the C/C++ standard.
Re: Executable return values
Quote:
Originally Posted by Axter
I recommend updating the above FAQ to add the system() function, and to clarify that _exec and _spawn are not part of the C/C++ standard.
Besides that this does not belong in this thread...you should have looked at the location of the FAQ. The FAQ is for Windows and not C or C++, thus, there is nothing misleading...nevertheless....since it is only 5 seconds of change...I will add the 'system()' function...
Re: Executable return values
Quote:
Originally Posted by Andreas Masur
Besides that this does not belong in this thread...you should have looked at the location of the FAQ. The FAQ is for Windows and not C or C++, thus, there is nothing misleading...nevertheless....since it is only 5 seconds of change...I will add the 'system()' function...
I'm not referring to it being misleading because of what thread it's in. If that was the case, I would have mention the other functions listed there.
I stated it's misleading, because there's a comment next _exec&_spawn stating "C run-time library".
This information is correct, but to a newbie, it could mislead he/she into believing it's part of the C/C++ standard.
That's why I mentioned it as a clarification, and not as a correction.
FYI: system is a function that works in Windows too....:cool:
Re: Executable return values
Quote:
Originally Posted by Axter
I'm not referring to it being misleading because of what thread it's in. If that was the case, I would have mention the other functions listed there.
I was referring to this thread...changing the content of the FAQ does have nothing to do with the question asked in this thread in the first place.
Quote:
Originally Posted by Axter
I stated it's misleading, because there's a comment next _exec&_spawn stating "C run-time library".
Yes...however, every function starting with '_' or '__' usually isn't part of the standard anyway... :cool:
Nevertheless, I have added some comment to these, thus, everybody should be satisfied by now...
Re: Executable return values
Quote:
Originally Posted by Axter
Here's a wrapper function that should be able to do what you want:
Code:
BOOL ExecuteAndWaitForCompletion ( LPCTSTR pszCmd)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory ( &si, sizeof ( STARTUPINFO));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
bRes = CreateProcess ( NULL,
pszCmd
NULL,
NULL,
TRUE,
NORMAL_PRIORITY_CLASS,
GetEnvironmentStrings (),
NULL,
&si,
&pi
);
WaitForSingleObject ( pi.hProcess, INFINITE);
CloseHandle( pi.hProcess);
CloseHandle( pi.hThread);
return ( bRes);
}
This is great thanks, exactly what I need. *I still need the return value from the launched .exe's main function, but I can look into how to do that possibly from here, even if I have to add some client/server code.
Cheers.
Edit: *I see this was already explained by Andreas - GetExitCodeProcess(). Thank you too, Andreas.