Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Questions (pretty basic)

Status
Not open for further replies.

djdy

Programmer
Aug 28, 2005
5
US
1. How do you end a Windows XP process using Delphi (ex. notepad.exe)?

2. How do you start another application from Delphi (ex. C:\Program\start.exe)?

Thank you!
 
For the second question:
ShellExecute(Handle, 'open', PChar('c:\test\app.exe'), nil, nil, SW_SHOW);

This will execute app.exe

As for the ending a process, maybe someone else here can help you.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
This function uses the createprocess API call, it gives you more control than using shellexecute.

Code:
{ Useage: Filename, The file to be executed
          Params, Any parameters that require passing to the file
          Timeout, Time to wait in mS. Pass the constant INFINITE to wait forever
          Visibility, Run window status e.g SW_SHOW, SW_MAXIMIZE
          It returns a string containing the last error if any
}
function WaitProcess(Filename: TFilename; Params: string;
         Timeout: DWORD; Visibility: integer): string;
var StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    WaitResult: integer;
begin
   ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
    with StartupInfo do
      begin
         cb := SizeOf(TStartupInfo);
         dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
         wShowWindow := Visibility;
       end;
    if fileexists(filename) or fileexists(params) then
      begin
         if (CreateProcess(nil, Pchar(Filename +' '+ Params), Nil, Nil,
                     False, NORMAL_PRIORITY_CLASS,
                     Nil, nil { pchar(extractfilepath(Params))}, StartupInfo, ProcessInfo))
            then
                begin
                    WaitResult := WaitForSingleObject(ProcessInfo.hProcess, Timeout);
                    if WaitResult = WAIT_TIMEOUT then
                       begin
                          result := 'Timeout has occured in waiting for result from compiler.';
                          TerminateProcess(ProcessInfo.hprocess, 1);
                          CloseHandle(ProcessInfo.hProcess);
                          CloseHandle(ProcessInfo.hThread);
                          exit;
                       end;
                    CloseHandle(ProcessInfo.hProcess);
                    CloseHandle(ProcessInfo.hThread);
                    Result := 'OK';
                end
            else
                result := 'Create Proccess failed ' + filename + ' '+ Params+ ' Error: '+inttostr(getlasterror);
      end
   else
      result := 'Process file does not exist';
end;

Steve: Delphi a feersum engin indeed.
 
Come to think of it this code also contains a means of terminating a process, but the process must be called from Delphi in order to have the required data for the ProcessInfo structure.

Code:
TerminateProcess(ProcessInfo.hprocess, 1);
                          CloseHandle(ProcessInfo.hProcess);
                          CloseHandle(ProcessInfo.hThread);



Steve: Delphi a feersum engin indeed.
 
The first question was answered in
How to "kill" a program
thread102-1066111

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top