Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Starting and closing an application using Delphi

Posted on 1997-12-10
4
Medium Priority
?
285 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 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Screencast - Getting to Know the Pipeline
Suggested Courses

963 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