CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2003
    Posts
    53

    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.

  2. #2
    Join Date
    Jun 2002
    Posts
    936

    Re: Executable return values

    Try shellexecute or shellexecuteex, you should get a return value.

  3. #3
    Join Date
    Mar 2003
    Posts
    53

    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?

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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.

  5. #5
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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);
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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()'...

  7. #7
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...
    Last edited by Andreas Masur; January 4th, 2005 at 05:07 PM.

  9. #9
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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....
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...

    Nevertheless, I have added some comment to these, thus, everybody should be satisfied by now...

  11. #11
    Join Date
    Mar 2003
    Posts
    53

    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.
    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
  •  





Click Here to Expand Forum to Full Width

Featured