ShellExecute from a Delphi service but the application must be visible

Andrea Matthiae
Andrea Matthiae used Ask the Experts™
on
Hi.
I have many problem to run a .bat (with a 'start cmd /c myapplication.exe' ) from a service. I have try ShellExecute but i have myapplication.exe running in background and never appears. My appilcation.exe is an APPTYPE CONSOLE.

I have try:
ShellExecute(0, 'open', 'c:\mydir\my.bat', nil, nil, SW_SHOWNORMAL) ;
or similar but this don't work!

I know that is difficult to execute a visual application from a service.
Can someone help me?

Windows 10
RAD Studio 10.2

Thanks in advice.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
1. Why are you not starting your program directly?
2. You can call bat file in cmd like:
ShellExecute(HInstance, PChar('open'), PChar('cmd'),
    PChar('/c "' + BatFile + '"'), PChar(ExtractFileDir(BatFile)), SW_SHOWNORMAL);

Open in new window

Andrea MatthiaeSoftware engineering

Author

Commented:
Hi Abhishek Sharma.

With your code the application start but is not visible; in the Windows Task Manager is in the "Backround Process list".
Yet again, did you run that exe to see if it creates console and not free it?

Also, if you're opening it via CMD, "/c" starts it and exits, try after removing it.

Also, for running it directly, try this:

procedure ShellExecAndWait(sExe, sCommandLine, sWorkDir: string;
  Wait, Freeze: Boolean; Show: Boolean = True);
var
  exInfo: TShellExecuteInfo;
  Ph: DWORD;
  WorkDir: PChar;
begin
  if sWorkDir = '' then
    WorkDir := nil
  else
    WorkDir := PChar(WorkDir);
  FillChar(exInfo, sizeof(exInfo), 0);
  with exInfo do
  begin
    cbSize := sizeof(exInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
    Wnd := GetActiveWindow();
    exInfo.lpVerb := 'open';
    exInfo.lpParameters := PChar(sCommandLine);
    lpFile := PChar(sExe);
    lpDirectory := WorkDir;
    nShow := SW_SHOW;
  end;
  if ShellExecuteEx(@exInfo) then
    Ph := exInfo.hProcess
  else
  begin
    exit;
  end;
  if Wait then
    while WaitForSingleObject(exInfo.hProcess, 0) <> WAIT_OBJECT_0 do
    begin
      if Freeze = false then
        Sleep(1);
    end;
  CloseHandle(Ph);
end;

Open in new window

Oracle dba
Top Expert 2009
Commented:
that's normal. not being visible.
a service is a background non-visible item.  It doens't have a front end.
the only interaction you can have with it, is starting and stopping it.

if you want your program to be visible, then don't start it via the service.
for instance, write a form application and start it with that.

on the programming side, you can interact with messages and things a like, but still, not visible

if you want to know what your program does, let it write a log file
and examine that log file.
Andrea MatthiaeSoftware engineering

Author

Commented:
Thanks Geert, I was hoping there is a possible solution but your answer confirms that it is impossible.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial