Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-02-2019, 12:43 PM
Luiz Eduardo Luiz Eduardo is offline
Member
 
Join Date: May 2014
Posts: 40
Question How to execute a batch file via ShellExecute from an app launched by a service?

I have a simple VCL form app that is executed in the NT AUTHORITY\SYSTEM account via a service app.

I have a batch file that will finalize the service and VCL processes and then delete the EXE files..

To execute the batch file, I'm using ShellExecute():

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
 ShellExecute(0, 'open',
        PChar(IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + 'delete.bat'),
        nil, PChar(ExtractFilePath(ParamStr(0))), SW_SHOWNORMAL);
end;
Here is the batch file:

Code:
@echo off
set EXE=MyService.exe
set EXE2=Project1.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE2%"') DO IF %%x == %EXE2% goto FOUND2
:FOUND
taskkill /F /IM MyService.exe
ping 127.0.0.1 -n 1 > nul
del /f "*.exe"
ping 127.0.0.1 -n 1 > nul
del /f "*.txt"
del /f "*.cur"
ping 127.0.0.1 -n 1 > nul
del /f "*.bmp"
ping 127.0.0.1 -n 1 > nul
del /f "*.jpg"
ping 127.0.0.1 -n 1 > nul
del /f "*.bat"
cls
exit
:FOUND2
taskkill /F /IM Project1.exe
ping 127.0.0.1 -n 1 > nul
del /f "*.exe"
ping 127.0.0.1 -n 1 > nul
del /f "*.txt"
del /f "*.cur"
ping 127.0.0.1 -n 1 > nul
del /f "*.bmp"
ping 127.0.0.1 -n 1 > nul
del /f "*.jpg"
ping 127.0.0.1 -n 1 > nul
del /f "*.bat"
cls
exit
The trouble is when I call ShellExecute() from the VCL app, a cmd.exe window appears but nothing happens (like if something locks the execution of code inside the batch file).

This is what happens when all is executed:



Someone could help with a solution?
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:53 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.