Why can't ShellExecute find the file?

coming from the * nix world, I am very confused with the behavior of Windows and probably with its security system.

I am just trying to execute an external program in my application. I found a WinExI ShellExecute function that works as expected, except for running some programs hosted in the% windir% \ System32 subdirectory.

  • Running ping.exe successfully

    ShellExecute(NULL, "open", "c:\\Windows\\System32\\ping.exe', NULL, NULL, SW_SHOW) ); // ^^^ OK, retcode == 42 
  • Java.exe execution not working

     ShellExecute(NULL, "open", "c:\\Windows\\System32\\java.exe', NULL, NULL, SW_SHOW) ); // ^^^ ERROR_FILE_NOT_FOUND, retcode == 2 

This is very strange because java.exe exists in System32 , has read / execute permissions for the Users group, and can be called from cmd.

 C:\>dir /qc:\Windows\System32\java.exe Volume in drive C has no label. Volume Serial Number is 56E3-0868 Directory of c:\Windows\System32 11.01.2012 23:40 172 320 NT AUTHORITY\SYSTEM java.exe 1 File(s) 172 320 bytes 0 Dir(s) 226 127 564 800 bytes free C:\>cacls c:\Windows\System32\java.exe c:\Windows\System32\java.exe NT AUTHORITY\SYSTEM:F BUILTIN\Administrators:F BUILTIN\Users:R 

What am I missing here?

OS is Windows 7 Home Edition.

Update:. If I copy c: \ Windows \ Sytem32 \ calc.exe to c: \ Windows \ Sytem32 \ calc2.exe, ShellExecute can run the original calc.exe file, but with the error calc2.exe, although the files are identical! The only difference is the additional permissions for the TrustedInstaller group, in which calc2.exe and java.exe are missing. Coincidence?

+4
source share
3 answers

Are you using a 64-bit operating system?

If so, C:\Windows\System32 will contain 64-bit binaries, and C:\Windows\SysWOW64 will contain 32-bit binaries (yes, it really is). For backward compatibility reasons, when starting 32-bit processes, Windows redirects access to C:\Windows\System32 to C:\Windows\SysWOW64 .

So, if you use a 32-bit process to view C:\Windows\System32 , you really see that in C:\Windows\SysWOW64 .

You can call the Wow64DisableWow64FsRedirection function to disable this behavior. Pay attention to the warning in the documentation and think carefully whether it applies to your case:

Note. The Wow64DisableWow64FsRedirection function affects all file operations performed by the current thread, which may have unintended consequences if file system redirection is disabled for any period of time. For example, loading a DLL depends on file system redirection, so disabling file system redirection will cause the DLL to fail to load. In addition, many function implementations use delayed loading and redirect failure. The failure state of the initial operation with a delay is preserved, so any subsequent use of the delay-load function will not succeed even after the file system is redirected again. To avoid these problems, disable file system redirection immediately before calls to certain file I / O functions (for example, CreateFile ) that cannot be redirected, and reactivate file system redirection immediately after using Wow64RevertWow64FsRedirection .

+10
source

Use ProcessMonitor to find out which files are being accessed, as well as what happens to the file operation and why.

+1
source

Check your environment settings, for example. "TRACK". Windows supports a separate environment for the system and users. Perhaps the DLL required by Java.exe is listed in only one environment, and when you run it through ShellExecute, it uses a different environment.

0
source

Source: https://habr.com/ru/post/1390656/


All Articles