Why ShellExecute can not find a file?

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

as comming from a *nix world I'm very confused with Windows behaviour and probably its security system.

I'm simply trying to execute an external program within my app. I've found the WinAPI function ShellExecute which works as expected except when launching some programs placed in %windir%\System32 subdirectory.

  • execution of ping.exe succeeds

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

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

It's very strange because java.exe does exist in System32, has read/execute permissions for Users group and can be invoked from cmd.

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 original calc.exe but fails with calc2.exe although files are identical !! The only difference are additional permissions for TrustedInstaller group which calc2.exe and also java.exe are missing. A coincidence ?

回答1:

Are you running a 64 bit operating system?

If so, C:\Windows\System32 will contain 64 bit binaries while C:\Windows\SysWOW64 will contain 32 bit binaries (yes, it really is that way around). For backwards compatibility reasons, when running 32 bit processes, Windows redirects access to C:\Windows\System32 to C:\Windows\SysWOW64.

So if you're using a 32 bit process to look at C:\Windows\System32, you're actually seeing what's in C:\Windows\SysWOW64.

You can call the Wow64DisableWow64FsRedirection function to disable this behavior. Do note the warning in the documentation and consider carefully whether it applies to your case:

Note: The Wow64DisableWow64FsRedirection function affects all file operations performed by the current thread, which can have unintended consequences if file system redirection is disabled for any length of time. For example, DLL loading depends on file system redirection, so disabling file system redirection will cause DLL loading to fail. Also, many feature implementations use delayed loading and will fail while redirection is disabled. The failure state of the initial delay-load operation is persisted, so any subsequent use of the delay-load function will fail even after file system redirection is re-enabled. To avoid these problems, disable file system redirection immediately before calls to specific file I/O functions (such as CreateFile) that must not be redirected, and re-enable file system redirection immediately afterward using Wow64RevertWow64FsRedirection.



回答2:

Use ProcessMonitor to figure out which files are being accessed and what file operation fails and why.



回答3:

Check the environment settings, e.g. "PATH". Windows keeps a separate environment for the system and for users. It might be that the DLL's needed by Java.exe are only listed in one environment and when you are running it through ShellExecute it is using the other environment.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!