ShellExecute from a Delphi service but the application must be visible

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.
Andrea MatthiaeSoftware engineeringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Abhishek SharmaCommented:
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 engineeringAuthor 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".
Abhishek SharmaCommented:
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);
  exInfo: TShellExecuteInfo;
  Ph: DWORD;
  WorkDir: PChar;
  if sWorkDir = '' then
    WorkDir := nil
    WorkDir := PChar(WorkDir);
  FillChar(exInfo, sizeof(exInfo), 0);
  with exInfo do
    cbSize := sizeof(exInfo);
    Wnd := GetActiveWindow();
    exInfo.lpVerb := 'open';
    exInfo.lpParameters := PChar(sCommandLine);
    lpFile := PChar(sExe);
    lpDirectory := WorkDir;
    nShow := SW_SHOW;
  if ShellExecuteEx(@exInfo) then
    Ph := exInfo.hProcess
  if Wait then
    while WaitForSingleObject(exInfo.hProcess, 0) <> WAIT_OBJECT_0 do
      if Freeze = false then

Open in new window

Geert GOracle dbaCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrea MatthiaeSoftware engineeringAuthor Commented:
Thanks Geert, I was hoping there is a possible solution but your answer confirms that it is impossible.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.