1

I have a vbs file that need to be run in 32-bit, even though I am running Windows 7 64-bit. I can launch this file with the command

C:\Windows\SysWOW64\cscript.exe my-file.vbs

and that works fine, but it leaves me with a redundant command prompt window that I have to close manually each time. It also makes it very cumbersome to run this vbs-file as a startup item.

Is there a way to start my 32-bit vbs-file in the background?

koenig
  • 516
  • 1
  • 6
  • 15

3 Answers3

3

Try this for the 64bit problem, if it works you can combine it with the other answers

EDIT: here a question that goes more in depth on the 32/64 bit issue

How do I check if wscript/cscript runs on x64 host OS?

here the modified version, should make sure the script runs on 64bit platform

On Error Resume Next
Dim WshShell, OsType
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
If OsType = "x86" then
  wscript.echo "Windows 32bit system detected"
else
  wscript.echo "Windows 64bit system detected"
  If InStr(LCase(WScript.FullName),"system32") Then 
    CreateObject("WScript.Shell").Run """%systemroot%\SysWOW64\wscript.exe"" """ & WScript.ScriptFullName & """" 
    Wscript.Quit 
  End If 
end if

Msgbox("I ran..")
Community
  • 1
  • 1
peter
  • 41,770
  • 5
  • 64
  • 108
  • This was the way to go, although I had to turn the check upside down... :-) – koenig May 23 '12 at 13:09
  • do you mean you used "If not Instr".. ? then i can adjust my answer.. can't test it out myself, have no 64bit client – peter May 23 '12 at 13:36
  • No, I checked for "system32" (which is 64bit, go figure) in the InStr and if so, called CreateObject with "SysWOW64" instead of "sysnative". It works now on 64bit in that the script relaunches itself in 32bit as per your recommendation; what remains is to make it general so the same script can be run on 32bit AND 64bit and launch the correct version of wscript. – koenig May 24 '12 at 07:21
  • @koenig: updated the answer for reference, could you try if it works ? – peter May 24 '12 at 07:59
  • For 64bit you still need the Wscript.Quit after CreateObject; apart from that it works fine. Thanks! – koenig May 24 '12 at 08:25
1

If you need to use cscript this is IMHO a cool solution

Const HIDDEN_WINDOW = 0

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW

Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")

objProcess.Create "Cscript.exe h:\Script\Test1.vbs", null, objConfig, intProcessID
peter
  • 41,770
  • 5
  • 64
  • 108
  • I tried to enter this code into a new file, test.vbs, and let the last line point to my vbs file. Nothing happened at all, I'm afraid. – koenig May 23 '12 at 12:19
  • must be the 64bit problem, i tried it first on my Vista 32bit system and it worked like a charm, allready tried my third solution ? – peter May 23 '12 at 12:37
0

If you can use wscript you could do the following, it's the simplest approach

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "Wscript.exe h:\Script\Test1.vbs"

actually you can do it as a one-liner (i'm a Ruby guy 8>)

CreateObject("Wscript.Shell").Run("Wscript.exe h:\Script\Test1.vbs")
peter
  • 41,770
  • 5
  • 64
  • 108
  • In the 64-bit world, this invokes the 64-bit version of wscript.exe if I am not mistaken. The error message I get when I try this approach seems to point towards the same conclusion. – koenig May 23 '12 at 11:10
  • what error do you get ? see this article http://blogs.msdn.com/b/joshpoley/archive/2008/09/18/running-32bit-dependent-scripts-in-a-64bit-world.aspx – peter May 23 '12 at 11:21
  • please try the solution in my third answer – peter May 23 '12 at 11:25