We help IT Professionals succeed at work.

ShellExecute from a Delphi service but the application must be visible

Andrea Matthiae
on
922 Views
Last Modified: 2018-10-03
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

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
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Andrea MatthiaeSoftware engineering

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions