Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Execute a file by its extension and wait to finish Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
02-Nov-02
Category
Shell API
Language
Delphi 2.x
Views
155
User Rating
8
# Votes
1
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Radikal Q3 

Execute/open any file with the associated application, waiting until it finish.

Answer:

We will make it thanks to the function of the API ShellExecuteEx 
 
Here the code is: 
 
Add 'ShellApi' in the uses of your form

1   
2   procedure TForm1.Button1Click(Sender: TObject);
3    
4     procedure RunAndWaitShell(Ejecutable,
5                               Argumentos:string
6                               ;Visibilidad:integer);
7     var 
8        Info:TShellExecuteInfo;
9        pInfo:PShellExecuteInfo;
10       exitCode:DWord;
11    begin 
12       {Puntero a Info}
13       {Pointer to Info}
14       pInfo:=@Info;
15       {Rellenamos Info}
16       {Fill info}
17       with Info do 
18       begin 
19        cbSize:=SizeOf(Info);
20        fMask:=SEE_MASK_NOCLOSEPROCESS;
21        wnd:=Handle;
22        lpVerb:=nil;
23        lpFile:=PChar(Ejecutable);
24        {Parametros al ejecutable}
25        {Executable parameters}
26        lpParameters:=Pchar(Argumentos+#0);
27        lpDirectory:=nil;
28        nShow:=Visibilidad;
29        hInstApp:=0;
30       end; 
31       {Ejecutamos}
32       {Execute}
33       ShellExecuteEx(pInfo);
34   
35       {Esperamos que termine}
36       {Wait to finish}
37       repeat 
38        exitCode := WaitForSingleObject(Info.hProcess,500);
39        Application.ProcessMessages;
40       until (exitCode <> WAIT_TIMEOUT);
41    end; 
42   
43  begin 
44    RunAndWaitShell('c:\windows\notepad.exe','c:\autoexec.bat',Sw_ShowNormal);
45  end; 


If we call to an executable, this it will be executed. 
If we call to a non executable file, the function will execute its associate 
application. 

For example, to open a file HTML with the default browser of the system:

RunAndWaitShell('c:\kk\registro.html', '', Sw_ShowNormal);

We can also execute and wait to finish a DOS program. 

For example, this opens my DOS editor QEdit to edit the Autoexec.bat: 
46  
47  RunAndWaitShell('c:\discoc\tools\q.exe', 'c:\autoexec.bat', Sw_ShowNormal);


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC