simple problem with waitforsingleobject

simple problem with waitforsingleobject

Post by RitzCod » Sat, 23 Feb 2002 02:25:35



Hi. I was hoping someone could help me with this problem.  I am trying
to use waitforsingleobject to:
1.  start program A
2. wait until the user closes program a
3. Start program b

For some reason this is not working - program b is launching as soon
as program a launches.  Any help would be greatly appreciated: The
code is below:

#include "stdafx.h"
#include "shellapi.h"

#include <stdio.h>
#include <winbase.h>

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

        HINSTANCE hres;
        HANDLE han;
        long ret;
        hres = ShellExecute( NULL, "open", "ProgramA.exe", NULL, "",
SW_SHOWMAXIMIZED );
        han = OpenProcess(SYNCHRONIZE, false, (DWORD)hres);
        ret = WaitForSingleObject(han, INFINITE);
        ret = CloseHandle(han);
// ProgramB.exe launches as soon as program a launches instead of
waiting until A is closed down!!
        hres = ShellExecute( NULL, "open", "ProgramB.exe", NULL, "",
SW_SHOWMAXIMIZED );
        return 0;

Quote:}

I build this using VC++ 6.0 latest Service Pack.  Thanks for any help!
 
 
 

simple problem with waitforsingleobject

Post by Svant » Sat, 23 Feb 2002 02:57:43



> Hi. I was hoping someone could help me with this problem.  I am trying
> to use waitforsingleobject to:
> 1.  start program A
> 2. wait until the user closes program a
> 3. Start program b

> For some reason this is not working - program b is launching as soon
> as program a launches.  Any help would be greatly appreciated: The
> code is below:

> #include "stdafx.h"
> #include "shellapi.h"

> #include <stdio.h>
> #include <winbase.h>

> int APIENTRY WinMain(HINSTANCE hInstance,
>                      HINSTANCE hPrevInstance,
>                      LPSTR     lpCmdLine,
>                      int       nCmdShow)
> {

>   HINSTANCE hres;
> HANDLE han;
> long ret;
> hres = ShellExecute( NULL, "open", "ProgramA.exe", NULL, "",
> SW_SHOWMAXIMIZED );
> han = OpenProcess(SYNCHRONIZE, false, (DWORD)hres);
> ret = WaitForSingleObject(han, INFINITE);
> ret = CloseHandle(han);
> // ProgramB.exe launches as soon as program a launches instead of
> waiting until A is closed down!!
> hres = ShellExecute( NULL, "open", "ProgramB.exe", NULL, "",
> SW_SHOWMAXIMIZED );
> return 0;
> }

Check the documentation for ShellExecute(). It does not return a Proc ID
which you need to do OpenProcess(). You could use ShellExecuteEx(),
and check the hProcess member of the SHELLEXECUTEINFO-struct
if I recall correctly.
Better is to use CreateProcess if you know the executable that you
want to run. You will anyway have problems in many cases - programs
do different things when started. Some will find an existing instance and
transfer control to that one for example - your wait will be short then...

If ProgramA.exe and ProgramB.exe are specific programs under your
control then all is fine - in general the problem is non-trivial.

I have written an ad-hoc "wait for application" that works in many cases,
where the application opens a file, but probably not all. You can find it in
module FileCmd.cpp, function LaunchApp() in http://axcrypt.sourceforge.net.

/Svante

 
 
 

1. problems with CreateProcess - WaitForSingleObject

I have developped an application (GUI), that acts like a shell.
That is, the user writes a command line, and a I, do the following:

1. Redirect the input, output and the errors, to pipes:
  with SUInfo do
  begin
    cb = sizeof(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    hStdOutput = PipeOutput;
    hStdError = PipeErrors;
    hStdInput = PipeInput;
    wShowWindow = SW_HIDE;
  end;

2. Create Process with
  CreateProcess(..,CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS,..., SUInfo,
...)

3. Wait for it completion, with:
WaitForSingleObject(ProcInfo.hProcess, INFINITE);

------------------------------------------
This goes fine on Win95/Win98.
With WinNT, it works fine if the execution application is GUI,
but if it is a console app. WaitForSingleObject, returns imediatly.
Example of comand line: 'cmd32.exe /p dir /s'.
I have tried SW_SHOWNORMAL, instead of SW_HIDE.
And also tried DETACHED_PROCESS, instead of CREATE_NEW_CONSOLE.
But with no sucess.

And tips would be appreciated
Thanks

2. PASSWORD

3. Problems using WaitForSingleObject

4. Which printer?

5. Problems with timeouts in WaitForSingleObject/WaitForMultipleObjects

6. Minimum Cable Length?

7. Synchronization problem using CreateProcess and WaitForSingleObject on 16-bit DOS app.

8. Problem with telnet --HELP--

9. Problem with WaitForSingleObject

10. Problem listening to COM port and WaitForSingleObject

11. Problem with reading COM port and WaitForSingleObject

12. Problems With WaitForSingleObject in Win95

13. problem with "THIS": simple problem!