Solved

Finding a instance after it is created with ShellExecute

Posted on 1999-01-25
6
156 Views
Last Modified: 2010-04-06
I want to open a DOS Window and execute a DOS program (such as the extract command) using the ShellExceute command. Since the window is hidden, it doesn't have a Window handle. (At least FindWindow can't find it). Is there a way, I can determin that the program is still running? I get a hInstance back from ShellExecute, but I can't find a function that is able to check if it is still vaild. Thanks for help...
0
Comment
Question by:jonnyfive
  • 3
  • 2
6 Comments
 

Author Comment

by:jonnyfive
ID: 1363252
Edited text of question
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1363253
Hi JonnyFive,

try this one:


unit runThread_;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, tools, ddraw,
  ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var Form1 : TForm1 = nil;

implementation

{$R *.DFM}

const unitName = 'runThread_.';

type TRunThread = class(TThread)
  private
    processHandle : cardinal;
    processReady  : boolean;
    waitingThread : cardinal;
    procedure Execute; override;
  end;

procedure TRunThread.Execute;
begin
  WaitForSingleObject(processHandle,INFINITE);   // This call does not return, unless copy is stopped
  processReady:=true;                            // Set "processReady" flag for main thread
  PostThreadMessage(waitingThread,WM_NULL,0,0);  // Wake up main thread
                                                 // If you call Application.HandleMessage (see below) in the
                                                 // main thread, the main thread is sleeping the most time in
                                                 // winAPI "waitMessage". So we send a "dummy" message in order
                                                 // to let the main thread return from Application.HandleMessage
end;

procedure TForm1.Button1Click(Sender: TObject);
var si : TStartupInfo;
    pi : TProcessInformation;
begin
  caption:='start copy...';
  ZeroMemory(@si,sizeOf(si)); si.cb:=sizeOf(si);
  si.dwFlags:=STARTF_USESHOWWINDOW; si.wShowWindow:=SW_HIDE;
  if CreateProcess(nil,'c:\command.com /c copy c:\autoexec.bat c:\test.bat >c:\output.txt',nil,nil,false,0,nil,nil,si,pi) then begin
    caption:='copy started...';
    with TRunThread.Create(true) do         // create the thread object, but do not start it now...
      try
        processHandle:=pi.hProcess;         // tell the thread what process it has to watch
        processReady:=false;                // flag for the loop (see below)
        waitingThread:=GetCurrentThreadID;  // the current threadID for the wakeup message (see above)
        caption:='wait for copy...';
        Resume;                             // now all information is prepared; so let's start the thread
        repeat
          Application.HandleMessage;        // message loop
        until Application.Terminated or processReady;  // continue with normal program when either the
                                                       // started process has stopped or our program is closed
        caption:='copy stopped...';
      finally Free end;
    CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
    caption:='ready...';
  end else caption:='could not start copy...';
end;

end.

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1363254
I was going to post some code, but Madshi's method is much better than mine.  I will copy his instead!  hehehe :)

Heath
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:jonnyfive
ID: 1363255
Hi Madshi... Thanks for the fast answer... I just tried the code. The reason why I want to know if the task is still running is simple: I want to wait for the Dos command to be finished before I go on in my program. Your code seems to work also parallel to the main program. What has to be changed to do this?
Thanks, Jonny Five :-)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1363256
It works *quite* parallel. The Button1Click procedure does not return until the dos process is finished, but messages are handled nevertheless. If you would not handle messages, it would have strange results. Your main form would not be repainted and such stuff.
I think you should use "enabled:=false" in the beginning of Button1Click and "enabled:=true" at the end of Button1Click. Then the user can't do anything with your form. I think that is exactly what you need...   :-)

Hi Heath, yeah, copy it. I like it if other experts like my sources...  :-)

Regards, Madshi.
0
 

Author Comment

by:jonnyfive
ID: 1363257
After I tested the code a bit more, I think it is a very nice solution to the problem. Thanks... :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now