Solved

Finding a instance after it is created with ShellExecute

Posted on 1999-01-25
6
157 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
control image tags in a string ? 12 129
DBGrid or StringGrid ? 6 88
how to change, disabled button color FMX ? 1 32
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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