Solved

Starting and closing an application using Delphi

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 94
Copy file in dll not working but working on exe ! 18 87
Using idhttp to login to instagram 2 76
Delphi Form ownership 4 70
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…
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 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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

22 Experts available now in Live!

Get 1:1 Help Now