CreateProcess - Running an app that runs an app?

I hope someone out there has an answer for this. I'm using CreateProcess in an application to Execute another
application (not mine) and this all works fine with the following code, the problem is that the application I
am executing in certain situations (about 75% of times) will itself execute another program, and when it
attempts to do this it just hangs, if I kill my program at this stage the it (the application mine started)
can then continue to run the program it's running without any problems. I've tried changing the creation flags
to CREATE_NEW_CONSOLE but no luck. So does anyone have any ideas, any help would be appreciated! BTW My application must wait until the other program terminates the program it runs and itself, and this must all work in WIN95/NT

Function WinExecAndWait(Path : string; Visibility : word) : word;
    STARTUPINFO : TStartupInfo;
    PROCESS_INFORMATION : TProcessInformation;
    FError : Integer;
    zCmdLine : Array [0..512] of Char;
     STARTUPINFO.wShowWindow := Visibility;
     StrPCopy(zCmdLine, Path);
  // Start the child process.
  if not (CreateProcess( Nil, // No module name (use command line).
      zCmdLine,            // Command line.
      Nil,             // Process handle not inheritable.
      Nil,             // Thread handle not inheritable.
      FALSE,            // Set handle inheritance to FALSE.
      0,                // No creation flags.
      Nil,             // Use parent’s environment block.
      Nil,             // Use parent’s starting directory.
      STARTUPINFO,          // Pointer to STARTUPINFO structure.
      PROCESS_INFORMATION )   // Pointer to PROCESS_INFORMATION structure.
    )   then
        DroneMain.Memo1.Lines.Add('CreateProcess failed: ' + StrPas(zCmdLine))
       DroneMain.Memo1.Lines.Add('Executing: ' + StrPas(zCmdLine));
       // Wait until child process exits.
       WaitForSingleObject( PROCESS_INFORMATION.hProcess, INFINITE );
    // Close process and thread handles.
    CloseHandle( PROCESS_INFORMATION.hProcess );
    CloseHandle( PROCESS_INFORMATION.hThread );
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

How about using ShellExecute? You can look in the API help, or there is an example in the delphi/demo/doc/filemanex (or something like that) directory. (It is the File Manager example program)
ShellExecute will execute another application and immediately return control to your app. The other app is started as if you loaded it from explorer or whatever. Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jsmith050497Author Commented:
Edited text of question
jsmith050497Author Commented:
Sorry, I should have mentioned that I need this to work in Windows 95 and it must also execute the program then wait for it to terminate before continuing. The code I supplied does work if the application run doesn't run another application, but if it does it all just stops and Win95's Task list reports that my program is not responding, when I end my programs task, the other program continues on fine, the strange thing is I get no errors, nothing, I thought maybe it was related to available environment space.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

What about using shellexecute, and continually checking the return value (the instance handle of the app) until it no longer  describes a valid application...
I think I answered this on news.... If that don't work out, let me know...

jsmith050497Author Commented:
Hi Erik,

Yes, no it did'nt work....I've replied with some more info I've found out to news about 5 mins ago, let me know if you have any ideas Re this.

jsmith050497Author Commented:
Hi Eric/Gysbert,

Okay, I think I've fixed it, here's what I did:

I removed:
WaitForSingleObject( PROCESS_INFORMATION.hProcess,
                  INFINITE );

and replaced it with:
  GetExitCodeProcess(PROCESS_INFORMATION.hProcess, fExitCode);
  while fExitCode = STILL_ACTIVE do
       GetExitCodeProcess(PROCESS_INFORMATION.hProcess, fExitCode);

All seems to operate fine now, just wish I could understand why WaitForSingleObject was causing the problem. Thankyou both for you input on this problem.

Best Regards
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.