如何在32位和64位Windows上从VBScript运行32位应用程序?

在VBScript中使用Shell.Run调用32位命令的最佳方法是什么,以便在Windows 32位和64位操作系统上都能成功?

在64位,应用程序终止,因为它不是一个64位的过程。 虽然我可以使用c:\​​ windows \ syswow64 \ cscript.exe myscript.vbs,但这不能移植到Windows 32位。

我无法重现您在我的系统上描述的问题。 如果我使用Shell.Run编写VBScript来调用%windir%\ syswow64中32位版本的记事本,那么尽pipe事实上脚本主机是64位,记事本是32位,但它工作得很好。

Set oShell = WScript.CreateObject("WScript.Shell") oShell.Run "%windir%\syswow64\notepad.exe" 

你试图调用什么32位命令失败?

我意识到这个问题是旧的,但我仍然发布一个答案,因为我有这方面的一些具体的,相关的知识,这对未来的Google员工(这是我碰到它)是有用的。

Microsoft在64位系统上提供了32位和64位版本的Windows脚本宿主( cscript.exewscript.exe ),运行脚本时默认为平台本机版本。 这是一个好消息,因为这意味着您可以自己处理平台差异,而无需担心在64位计算机上的32位仿真环境中WSH的外部​​行为。 如果您确实想要依赖此行为,则可以从\SysWOW64目录手动调用cscriptwscript ,但通常最好使用本机版本,并在自己的代码中处理平台差异。

我总是在写下所有的VBScript前言:

 Option Explicit 'I always use this to avoid spelling errors in var names; Just my personal preference Dim objShell :Set objShell = CreateObject("WScript.Shell") Dim objFS :Set objFS = CreateObject("Scripting.FileSystemObject") 'Determine OS platform Dim strPlatform :strPlatform = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") If strPlatform = "AMD64" Then strPlatform = "x64" 'Set 32-bit and 64-bit filesystem paths Dim strProgramFilesX64 'Will always be "\Program Files", regardless of platform Dim strProgramFilesX86 'Will be "\Program Files (x86)" on 64-bit, otherwise "\Program Files" on 32-bit Dim strSystem32X64 'Will always be "\Windows\System32", regardless of platform Dim strSystem32X86 'Will be "\Windows\SysWOW64" on 64-bit, otherwise "\Windows\System32" on 32-bit strProgramFilesX64 = objShell.ExpandEnvironmentStrings("%ProgramFiles%") strSystem32X64 = objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32" If strPlatform = "x64" Then strProgramFilesX86 = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%") strSystem32X86 = objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\SysWOW64" Else strProgramFilesX86 = strProgramFilesX64 strSystem32X86 = strSystem32X64 End If 

请注意,上面的代码使用shell环境variables来首先设置平台和64位path。 这是因为这些variables对于每个Windows版本都是通用的,但是x86特定的variables只在64位系统上定义(在我看来是倒退的)。

在脚本开始时使用这些代码使得它们在平台之间完全可移植。 您可以使用strProgramFilesX86/64strSystem32X86/64variables来控制自己的代码的行为,并且无论您的脚本运行在哪个平台上,它们都将始终映射到正确的位置。

例如,如果您使用objShell.Run()来启动您所知道的是32位软件,则可以始终使用objShell.Run(strProgramFilesX86 & "\MyApp\App.exe") ,并且它将正确运行从64位系统上的Program Files (x86)和32位系统上的\Program Files

你也可以使用strPlatformvariables,如果你需要在一个平台上发生不同的事情。 例如,当我正在进行软件安装时,我有一个32位和64位版本的软件。 我将32位版本重命名为在其文件名末尾有一个-x86 ,而在64位版本中同样是-x64 。 这样,我可以用一行代码调用安装程序,而不必关心它是什么平台,就像这样:

 objShell.Run "setup-" & strPlatform & ".exe" 

这将评估为64位系统上的setup-x64.exe和32位系统上的setup-x86.exe

只要你坚持这些variables,你就不用担心维护两个不同版本的脚本,或者目标计算机运行的是什么平台。