Solved

Starting and closing an application using Delphi

Posted on 1997-12-10
4
254 Views
Last Modified: 2010-04-04
How can I use delphi 2.0 to start and close down an application. Can you provide some code to do this?
0
Comment
Question by:okemo
4 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1353336
Example for starting the Notepad:
Winexec('c:\winNT\Notepad',SW_SHOW);

Example for closing the Notepad:
SendMessage(Findwindow('notepad',nil), WM_Close, 0, 0);

0
 

Author Comment

by:okemo
ID: 1353337
This may be the case for applications whose executable name is the same as the window name. How do you do this if the two are different because not applications open windows with the same name as the executable.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353338
Try this component,

I use it a lot : It's easy, great, well documented, with source, freeware,..., what else do you need?

http://SunSite.icm.edu.pl/delphi/ftp/d10free/execfl32.zip

http://SunSite.icm.edu.pl/delphi/ftp/d10free/execfile.zip (D1)

Have fun,
c.u. ZifNab;
0
 

Accepted Solution

by:
mshaer earned 50 total points
ID: 1353339
You can go straight into the API

because you want to start and end the process, you can use

CreateProcess and TerminateProcess

from the documentation :

BOOL CreateProcess(

    LPCTSTR  lpApplicationName,      // pointer to name of executable module
    LPTSTR  lpCommandLine,      // pointer to command line string
    LPSECURITY_ATTRIBUTES  lpProcessAttributes,      // pointer to process security attributes
    LPSECURITY_ATTRIBUTES  lpThreadAttributes,      // pointer to thread security attributes
    BOOL  bInheritHandles,      // handle inheritance flag
    DWORD  dwCreationFlags,      // creation flags
    LPVOID  lpEnvironment,      // pointer to new environment block
    LPCTSTR  lpCurrentDirectory,      // pointer to current directory name
    LPSTARTUPINFO  lpStartupInfo,      // pointer to STARTUPINFO
    LPPROCESS_INFORMATION  lpProcessInformation       // pointer to PROCESS_INFORMATION  
   );

BOOL TerminateProcess(

    HANDLE  hProcess,      // handle to the process
    UINT  uExitCode       // exit code for the process  
   );      

so, make a form with two buttons and the following procedures

procedure TForm1.Button1Click(Sender: TObject);
var
  si : TStartupInfo;       // delphi objects
begin
  FillChar(si, SizeOf(si), 0); // make all 0's (should fill in where required)
  si.cb := SizeOf(si);         // must always do this
  CreateProcess('c:\windows\notepad.exe', nil, nil, nil, TRUE, 0, nil, nil, si, pi);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  h :THandle;
begin
  h := Openprocess(PROCESS_TERMINATE, FALSE,pi.dwProcessId);
  TerminateProcess(h, 0);
end;

The first opens notepad, the second closes it.


This is of course a bit dangerous becuase child processes of
the process are not killed, but it should be fine for most things.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

16 Experts available now in Live!

Get 1:1 Help Now