Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Starting and closing an application using Delphi

Posted on 1997-12-10
4
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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