.net 如何从 32 位进程启动 64 位进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2003573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:44:31  来源:igfitidea点击:

How to start a 64-bit process from a 32-bit process

.netprocess32bit-64bitsyswow64

提问by Igor Zevaka

I am trying to run a 64 bit executable (java.exe) from our 32-bit .NET application. I am using Processclass and invoking cmd /c <command name>in order to support all possible commands (like dir, cdetc).

我正在尝试从我们的 32 位 .NET 应用程序运行 64 位可执行文件 (java.exe)。我使用的Process类和调用cmd /c <command name>,以支持所有可能的命令(如dircd等)。

The problem is that on my machine I installed 64-bit version of JRE and java.exe is only available from C:\Windows\System32folder (x64). I have tried explicily starting 64 bit version of cmd.exeby calling C:\Windows\System32\cmd.exebut it gets redirected to SysWOW64due to calling process being 32 bit.

问题是在我的机器上我安装了 64 位版本的 JRE,而 java.exe 只能从C:\Windows\System32文件夹 (x64) 中获得。我已经尝试cmd.exe通过调用显式启动 64 位版本,C:\Windows\System32\cmd.exeSysWOW64由于调用进程是 32 位,它被重定向到。

Is there anything else I can do to get this to work?

我还能做些什么来让它发挥作用?

EDITThe whole cmd /cthing is a bit of a red herring. It is not part of the problem, being able to run 64 bit executables is.

编辑整件事cmd /c有点像红鲱鱼。这不是问题的一部分,能够运行 64 位可执行文件才是。

回答by Michael

You can temporarily disable filesystem redirection around the call to Process.Start, the appropriate API's to P/Invoke are Wow64DisableWow64FsRedirectionand Wow64RevertWow64FsRedirection.

您可以在对 Process.Start 的调用周围暂时禁用文件系统重定向,P/Invoke 的适当 API 是Wow64DisableWow64FsRedirectionWow64RevertWow64FsRedirection

Another option is to use %windir%\sysnative, which is available on Windows Vista and above.

另一种选择是使用 %windir%\sysnative,它在 Windows Vista 及更高版本上可用。

回答by Nikolai

What you do is you use %windir%\sysnative to resolve 64-bit CMD.EXE and then you launch your other 64-bit program via "/c" command line option.

您所做的是使用 %windir%\sysnative 来解析 64 位 CMD.EXE,然后通过“/c”命令行选项启动其他 64 位程序。

回答by DenNukem

c:\>set proc
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 70 Stepping 1, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=4601

c:\>c:\windows\sysnative\cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\>set proc
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 70 Stepping 1, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=4601

c:\>

回答by Santhosh

Just in case this might help.. http://msdn.microsoft.com/en-us/library/aa384187(VS.85).aspx

以防万一这可能会有所帮助.. http://msdn.microsoft.com/en-us/library/aa384187(VS.85).aspx

Note that if the application is manifested to show the UAC prompt, then redirection will not take place. And also some folders are exempt from redirection.

请注意,如果应用程序显示为显示 UAC 提示,则不会发生重定向。还有一些文件夹免于重定向。

回答by Markus

"sysnative" seems to have some drawbacks.

“sysnative”似乎有一些缺点。

Example: When you start powershell.exe via C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exesome CmdLets like Get-AppxProvisionedPackage" and "Get-WindowsCapabilitydon't work / throw exceptions:

示例:当您通过C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe一些 CmdLet启动 powershell.exe 时,例如Get-AppxProvisionedPackage" and "Get-WindowsCapability不工作/抛出异常:

Get-AppxProvisionedPackage: "Error setting current directory to "C:\Windows\SysNative\WindowsPowerShell\v1.0": Part of the path "C:\Windows\SysNative\WindowsPowerShell\v1.0" could not be found"

(translated from German "Fehler beim Festlegen des aktuellen Verzeichnisses auf "C:\Windows\SysNative\WindowsPowerShell\v1.0": Ein Teil des Pfades "C:\Windows\SysNative\WindowsPowerShell\v1.0" konnte nicht gefunden werden.")

Get-AppxProvisionedPackage:“将当前目录设置为“C:\Windows\SysNative\WindowsPowerShell\v1.0 时出错”:找不到路径“C:\Windows\SysNative\WindowsPowerShell\v1.0”的一部分”

(翻译自德语“Fehler beim Festlegen des aktuellen Verzeichnisses auf "C:\Windows\SysNative\WindowsPowerShell\v1.0": Ein Teil des Pfades "C:\Windows\SysNative\WindowsPowerShell\v1.0" konnte nicht gefunden werden。 ”)

There might be similar problems running other processes than powershell (whenever the process source directory is important?) ...

运行 powershell 以外的其他进程可能会出现类似的问题(只要进程源目录很重要?)...