0

In myApplication.exe I call ShellExecute to start another process, let's say OtherApplication.exe.

In OtherApplication.exe, I'm noticing that it is failing to spawn it's own processes, for example cmd.exe to do some tasks.

If I run OtherApplication.exe from Windows Explorer (not from myApplication.exe) everything in OtherApplication.exe runs as expected.

I looked in Process Explorer and observed the following in the process tree:

  • Explorer.exe
    • myApplication.exe
      • OtherApplication.exe [this isn't working]
    • OtherApplication.exe [this is working]

So I guess my question is can I start a child process from myApplication.exe that doesn't show it as the parent? I couldn't think of any other reason why OtherApplication.exe would be behaving differently if I run it myself from Windows Explorer or from another app use ShellExecute.

I get the same result if I use something like from myApplication.exe:

ShellExecute(0, nil, 'cmd.exe', PWideChar('/C "' + '"C:\...somepaths...\OtherApplication.exe"'), nil, SW_HIDE);

then cmd.exe is a child of myApplication.exe. Its process children include conhost.exe and OtherApplication.exe. Which still doesn't behave the same as the explorer.exe process.

ikathegreat
  • 2,311
  • 9
  • 49
  • 80
  • What if you run OtherApplication from a console window, so the parent process is still cmd.exe, but your application isn't involved? What does the OtherApplication vendor have to say about the matter? – Rob Kennedy Mar 03 '16 at 22:24
  • "*can I start a child process from myApplication.exe that doesn't show it as the parent?*" - No. "*I couldn't think of any other reason why OtherApplication.exe would be behaving differently*" - there are many reasons why it could be. For instance, what user account is running `myApplication.exe` and does it have the permissions that `OtherApplication.exe` requires? Or maybe `OtherApplication.exe` is using environment/path values incorrectly and they are being affected when spawned by `myApplication.exe`. What is `OtherApplication.exe` actually failing on? Please provide more details. – Remy Lebeau Mar 03 '16 at 22:25
  • @RobKennedy I tested that too, just opening a command prompt window and running that exe. OtherApplication.exe still doesn't work correctly. – ikathegreat Mar 03 '16 at 22:28
  • @RemyLebeau I realize there are about a million possible causes and I'm try to eliminate the impossibilities. I can tell you that both applications are installed to a subfolder in `C:\Program Files (x86)` under the 1 and only user account for the PC. `OtherApplication.exe` is failing to run `cmd.exe`. I see that child process spawn when I run `OtherApplication.exe` from explorer. No difference if I use `ShellExecute` or start `myApplication.exe` from command prompt as admin. – ikathegreat Mar 03 '16 at 22:31
  • Does myApplication.exe run elevated? When the child fails, what is the error? It's important to gather information. – David Heffernan Mar 03 '16 at 22:57
  • 1
    @ikathegreat: Please show the actual code that `OtherApplication.exe` is using to spawn `cmd.exe`. It is clearly doing something wrong. Why are you spawning `cmd.exe` that you cannot do with native APIs? And why are you using `ShellExecute()` to run `.exe` files instead of using `CreateProcess()`? The *only* reason you should ever use `ShellExecute()` to spawn an `.exe` directly is if you need to use the `"runas"` verb to start a UAC-elevated process (and even then, you could use [`CreateProcessElevated()`](http://www.codeproject.com/Articles/19165/Vista-UAC-The-Definitive-Guide) instead). – Remy Lebeau Mar 03 '16 at 23:05
  • That's good news. It means it's not your fault. File a bug with OtherApplication's vendor and move on to more important problems in *your* product. – Rob Kennedy Mar 03 '16 at 23:30
  • 1
    perhaps the problem is in the current directory for 'OtherApplication.exe'. Check it in ProcessExplorer ->Process properties->Image tab. – kami Mar 04 '16 at 05:37
  • 2
    You're trying to fix the wrong problem -> figure out why OtherApplication.exe doesn't work. – Remko Mar 04 '16 at 09:08
  • @kami I can't believe it, but that was it. The Current Directory in ProcessExplorer was "C:\Windows\System32". I called `SetCurrentDir` on the `OtherApplication.exe` directory and it worked! – ikathegreat Mar 04 '16 at 14:59
  • 2
    That's good, but please pay attention to Remy Lebeau comment: use `CreateProcess` instead of `ShellExecute` – kami Mar 04 '16 at 15:22
  • 2
    Console applications take the working directory as an input. It's usually important to specify it. Inheriting your GUI app's working directory is not at all reliable. This is yet another reason to use the correct API for creating a process. `CreateProcess`. You specify the working directory as an argument. – David Heffernan Mar 04 '16 at 15:51

0 Answers0