13

I am trying to execute an EXE 'two.exe' from another application 'one.exe' in Delphi XE2 using ShellExecute.

ShellExecute(0, 'open', 'two.exe', nil, nil, SW_NORMAL);

It works perfectly, but whenever I terminate application 'one.exe' (the parent app) from Task Manager's process tab, using the "end process tree" option, the application 'two.exe' also gets terminated.

How can I prevent my ShellExecuted application from getting terminated like this?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
jimsweb
  • 1,082
  • 2
  • 17
  • 37
  • I can't reproduce what you're explaining... like you explain: `ShellExecute(Application.Handle, 'open', PWideChar(ExtractFileName(Application.ExeName)), nil, PWideChar(ExtractFilePath(Application.ExeName)), SW_SHOWNORMAL);` Don't kill the other instances... `ShellExecute(0, 'open', PWideChar(ExtractFileName(Application.ExeName)), nil, PWideChar(ExtractFilePath(Application.ExeName)), SW_SHOWNORMAL);` – Whiler May 15 '12 at 16:39
  • Are you sure there is not some other interaction between the two apps that would cause the second app to close when the first is closed? – hatchet - done with SOverflow May 15 '12 at 16:47
  • 2
    I can reproduce it. Exactly as described in the question. In Process Explorer there is a view of the processes that clearly shows the group hierarchy. – David Heffernan May 15 '12 at 16:50
  • @DavidHeffernan: I agree with Process Explorer.. but with the regular TaskManager, when I end the process tree, it doesn't kill my child app if my first argument is 0... – Whiler May 15 '12 at 16:59
  • `CREATE_NEW_PROCESS_GROUP` looks like the thing to use. But I can't make it work. – David Heffernan May 15 '12 at 17:04
  • 2
    +1. Just as a note: Unless you know for sure that the file association for a file extension has a specific verb assigned, don't use one. (For instance, .exe in HKCR does not have an `open` verb, it has `run`. You shouldn't use `open`.) Just use `nil` instead when you want the default action to take place, and let Windows figure out what to do; it will automatically open Word document (`.doc`) files, run executables, etc. – Ken White May 15 '12 at 19:47

2 Answers2

12

Ok, not very nice solution... but tested with success ;o)

ShellExecute(0, 'open', 'cmd', 
  PChar('/C  start "" "' + Application.ExeName + '"'), nil, SW_HIDE);

enter image description here

The first is manually launched... his child with regular shellexecute call... The last one, with the shellexecute + cmd trick...

/C explanation

Community
  • 1
  • 1
Whiler
  • 7,998
  • 4
  • 32
  • 56
9

I think you need to create an intermediate process which in turn spawns two.exe. You then need the intermediate process to terminate immediately, at which point the relationship between the original process and two.exe is broken.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    After some search without result, I agree as I don't think it's possible to update the [th32ParentProcessID](http://stackoverflow.com/a/1857762/596852) which is probably used by Process Explorer... – Whiler May 15 '12 at 18:18