|
-
January 4th, 2005, 10:48 AM
#1
Executable return values
Is it possible to launch an executable in code using something like CreateProcess, but to make my program wait and not continue until the launched program has closed and returned a value?
I need to launch another .exe and get it's return value when it closes without having to create a client/server type setup.
Cheers.
-
January 4th, 2005, 11:07 AM
#2
Re: Executable return values
Try shellexecute or shellexecuteex, you should get a return value.
-
January 4th, 2005, 12:25 PM
#3
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?
-
January 4th, 2005, 12:31 PM
#4
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.
-
January 4th, 2005, 01:37 PM
#5
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);
}
-
January 4th, 2005, 04:04 PM
#6
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()'...
-
January 4th, 2005, 04:34 PM
#7
Re: Executable return values
 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.
-
January 4th, 2005, 05:00 PM
#8
Re: Executable return values
 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...
Last edited by Andreas Masur; January 4th, 2005 at 05:07 PM.
-
January 4th, 2005, 05:22 PM
#9
Re: Executable return values
 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....
-
January 4th, 2005, 05:31 PM
#10
Re: Executable return values
 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.
 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... 
Nevertheless, I have added some comment to these, thus, everybody should be satisfied by now...
-
January 5th, 2005, 05:12 AM
#11
Re: Executable return values
 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.
Last edited by Jim1981; January 5th, 2005 at 05:16 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|