?
Solved

CreateProcess - Running an app that runs an app?

Posted on 1997-05-04
7
Medium Priority
?
353 Views
Last Modified: 2010-04-06
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;
var
    STARTUPINFO : TStartupInfo;
    PROCESS_INFORMATION : TProcessInformation;
    FError : Integer;
    zCmdLine : Array [0..512] of Char;
begin
     FillChar(StartupInfo,Sizeof(StartupInfo),#0);
     STARTUPINFO.cb := sizeof(STARTUPINFO);
     STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
     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))
  else
  begin
       DroneMain.Memo1.Lines.Add('Executing: ' + StrPas(zCmdLine));
       // Wait until child process exits.
       WaitForSingleObject( PROCESS_INFORMATION.hProcess, INFINITE );
  end;
    // Close process and thread handles.
    CloseHandle( PROCESS_INFORMATION.hProcess );
    CloseHandle( PROCESS_INFORMATION.hThread );
end;
0
Comment
Question by:jsmith050497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
gysbert1 earned 200 total points
ID: 1335908
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.
0
 

Author Comment

by:jsmith050497
ID: 1335909
Edited text of question
0
 

Author Comment

by:jsmith050497
ID: 1335910
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.

James
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:gysbert1
ID: 1335911
What about using shellexecute, and continually checking the return value (the instance handle of the app) until it no longer  describes a valid application...
0
 
LVL 3

Expert Comment

by:sperling
ID: 1335912
I think I answered this on news.... If that don't work out, let me know...


Erik.
0
 

Author Comment

by:jsmith050497
ID: 1335913
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.

James
0
 

Author Comment

by:jsmith050497
ID: 1335914
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
  begin
        Application.ProcessMessages;
       GetExitCodeProcess(PROCESS_INFORMATION.hProcess, fExitCode);
  end;

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
James
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question