Solved

Starting and closing an application using Delphi

Posted on 1997-12-10
4
250 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

15 Experts available now in Live!

Get 1:1 Help Now