Force vbscript to open command line on 64-bit instead of 32-bit
I've been trying to get this script to work all day!
Here are some facts about my situation ...
- I have a program named "ffmpeg.exe" in the "C: \ Windows \ System32" folder.
- I do not have this program in the "C: \ Windows \ SysWOW64 \" folder.
This is currently the script I have ...
Option Explicit
Dim oFSO, oShell, sCommand
Dim sFilePath, sTempFilePath
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFilePath = "C:\test\in_video.mkv"
sTempFilePath = "C:\test\out_video.mp4"
sCommand = "%comspec% /k ffmpeg -n -i """ + sFilePath + """ -c:v copy -c:a copy """ + sTempFilePath + """"
WScript.Echo sCommand
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run sCommand, 1, True
Set oShell = Nothing
Set oFSO = Nothing
If I run this script manually on the command line, then it seems to work fine. But if I let another application run (like uTorrent in this case) it runs the script as expected, but when it tries to process the oShell.Run command it runs in a 32 bit environment! Then I get this ...
If I try to open a new command line (nothing fancy) it seems to me that the default is 64-bit, and then I can type "ffmpeg" and it will show me the help content as expected.
So for some reason I cannot get the script to run applications (specifically CMD) in a 64 bit environment. Does anyone know how I can achieve this?
Update
It seems that my script is actually running in 32 bit mode! Even though the script title bar says "C: \ Windows \ System32 \ cscript.exe", this is a 64 bit environment!
I used the following script to determine that it works in a 32 bit environment ...
Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture
Set WshShell = CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")
process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE")
If process_architecture = "x86" Then
system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")
If system_architecture = "" Then
system_architecture = "x86"
End if
Else
system_architecture = process_architecture
End If
WScript.Echo "Running as a " & process_architecture & " process on a " _
& system_architecture & " system."
source to share
If it's only for cmd or some file in System32, you can use sysnative as suggested in the comment. This will result in 64Bit System32 even from 32Bit executables. Just replace "system32" with "sysnative" for each use. (unfortunately this is not present on 32-bit windows, so you need to check if you are using the script for systems with both architectures ...)
If you have a lot of access or using com objects, I found it easier to use this same method to restart the script. The following code:
If fso.FileExists("C:\Windows\SysWOW64\wscript.exe") Then ' very basic check for 64bit Windows, you can replace it with a more complicated wmi check if you find it not reliable enough
If InStr(1, WScript.FullName, "SysWOW64", vbTextCompare) <> 0 Then ' = case insensitive check
newFullName = Replace(WScript.FullName, "SysWOW64", "Sysnative", 1, -1, vbTextCompare) ' System32 is replaced by Sysnative to deactivate WoW64, cscript or wscript stay the same
newArguments = "" ' in case of command line arguments they are passed on
For Each arg In WScript.Arguments
newArguments = newArguments & arg & " "
Next
wso.Run newFullName & " """ & WScript.ScriptFullName & """ " & newArguments, , False
WScript.Quit '32 Bit Scripting Host is closed
End If
End If
Basically closes the script whenever it is called with a 32bit scripting host and restarts it with a 64bit one so that everything will be found where expected.
source to share