Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 02-04-2019, 04:20 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,325
Default

This code should work to execute a batch file.
Problem is that in your batchfile you try to kill the service itself, that's not allowed.
Reply With Quote
  #3  
Old 02-08-2019, 01:51 PM
Luiz Eduardo Luiz Eduardo is offline
Member
 
Join Date: May 2014
Posts: 40
Lightbulb

Quote:
Originally Posted by Norrit View Post
This code should work to execute a batch file.
Problem is that in your batchfile you try to kill the service itself, that's not allowed.
I discovered that trouble is because the batch file also is executed in debug mode:

Code:
dwCreationFlags = DEBUG_PROCESS;
So, batch file fails to delete executables after kill because he still is linked with debug of some way.


Here was the final solution:

Code:
dwCreationFlags = DEBUG_ONLY_THIS_PROCESS;

Last edited by Luiz Eduardo; 02-08-2019 at 01:54 PM.
Reply With Quote
Reply

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 08:50 PM.


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