如何在64位机器上以32位模式运行VBScript?
我有一个文本文件,以.vbs结尾,我写了下面的内容:
Set Conn = CreateObject("ADODB.Connection") Conn.Provider = "Microsoft.ACE.OLEDB.12.0" Conn.Properties("Data Source") = "C:\dummy.accdb" Conn.Properties("Jet OLEDB:Database Password") = "pass" Conn.Open Conn.Close Set Conn = Nothing
- 当我在Windows 32位机器上执行它时,它运行并结束,没有任何概念(预期)。
- 当我在Windows 64位机器上执行此操作时,会出现错误
供应商无法find。 它可能没有正确安装。
但它被安装。 我认为问题的根源在于提供者是一个32位的提供者,据我所知它并不是以64位的forms存在的。
如果我通过IIS在64位机器上运行VBScript(作为ASP文件),我可以select它应该以32位模式运行。 它可以find提供者。
我怎样才能find在Windows 64位提供商? 我可以告诉CScript(它执行.vbs文本文件)以某种方式运行在32位模式?
按照http://support.microsoft.com/kb/896456
要启动一个32位命令提示符,请按照下列步骤操作:
* Click Start, click Run, type %windir%\SysWoW64\cmd.exe, and then click OK.
然后键入
cscript vbscriptfile.vbs
' C:\Windows\System32\WScript.exe = WScript.exe Dim ScriptHost : ScriptHost = Mid(WScript.FullName, InStrRev(WScript.FullName, "\") + 1, Len(WScript.FullName)) Dim oWs : Set oWs = CreateObject("WScript.Shell") Dim oProcEnv : Set oProcEnv = oWs.Environment("Process") ' Am I running 64-bit version of WScript.exe/Cscript.exe? So, call script again in x86 script host and then exit. If InStr(LCase(WScript.FullName), LCase(oProcEnv("windir") & "\System32\")) And oProcEnv("PROCESSOR_ARCHITECTURE") = "AMD64" Then ' rebuild arguments If Not WScript.Arguments.Count = 0 Then Dim sArg, Arg sArg = "" For Each Arg In Wscript.Arguments sArg = sArg & " " & """" & Arg & """" Next End If Dim sCmd : sCmd = """" & oProcEnv("windir") & "\SysWOW64\" & ScriptHost & """" & " """ & WScript.ScriptFullName & """" & sArg WScript.Echo "Call " & sCmd oWs.Run sCmd WScript.Quit End If
如果你有控制运行cscript可执行文件,然后运行32位实现的X:\windows\syswow64\cscript.exe
版本。
' *************** ' *** 64bit check ' *************** ' check to see if we are on 64bit OS -> re-run this script with 32bit cscript Function RestartWithCScript32(extraargs) Dim strCMD, iCount strCMD = r32wShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\SysWOW64\cscript.exe" If NOT r32fso.FileExists(strCMD) Then strCMD = "cscript.exe" ' This may not work if we can't find the SysWOW64 Version strCMD = strCMD & Chr(32) & Wscript.ScriptFullName & Chr(32) If Wscript.Arguments.Count > 0 Then For iCount = 0 To WScript.Arguments.Count - 1 if Instr(Wscript.Arguments(iCount), " ") = 0 Then ' add unspaced args strCMD = strCMD & " " & Wscript.Arguments(iCount) & " " Else If Instr("/-\", Left(Wscript.Arguments(iCount), 1)) > 0 Then ' quote spaced args If InStr(WScript.Arguments(iCount),"=") > 0 Then strCMD = strCMD & " " & Left(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), "=") ) & """" & Mid(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), "=") + 1) & """ " ElseIf Instr(WScript.Arguments(iCount),":") > 0 Then strCMD = strCMD & " " & Left(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), ":") ) & """" & Mid(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), ":") + 1) & """ " Else strCMD = strCMD & " """ & Wscript.Arguments(iCount) & """ " End If Else strCMD = strCMD & " """ & Wscript.Arguments(iCount) & """ " End If End If Next End If r32wShell.Run strCMD & " " & extraargs, 0, False End Function Dim r32wShell, r32env1, r32env2, r32iCount Dim r32fso SET r32fso = CreateObject("Scripting.FileSystemObject") Set r32wShell = WScript.CreateObject("WScript.Shell") r32env1 = r32wShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") If r32env1 <> "x86" Then ' not running in x86 mode For r32iCount = 0 To WScript.Arguments.Count - 1 r32env2 = r32env2 & WScript.Arguments(r32iCount) & VbCrLf Next If InStr(r32env2,"restart32") = 0 Then RestartWithCScript32 "restart32" Else MsgBox "Cannot find 32bit version of cscript.exe or unknown OS type " & r32env1 Set r32wShell = Nothing WScript.Quit End If Set r32wShell = Nothing Set r32fso = Nothing ' ******************* ' *** END 64bit check ' *******************
将上面的代码放在脚本的开头,随后的代码将以32位模式运行,并可以访问32位ODBC驱动程序。 来源 。
在启动脚本中,你可以强制它,它允许两个架构保持相同的脚本和相同的启动器
:: For 32 bits architecture, this line is sufficent (32bits is the only cscript available) set CSCRIPT="cscript.exe" :: Detect windows 64bits and use the expected cscript (SysWOW64 contains 32bits executable) if exist "C:\Windows\SysWOW64\cscript.exe" set CSCRIPT="C:\Windows\SysWOW64\cscript.exe" %CSCRIPT% yourscript.vbs
在64位机器上运行32位脚本的替代方法:%windir%\ syswow64 \ cscript.exe vbscriptfile.vbs
我们可以强制vbscript始终以32位模式运行,通过在“Computer \ HKLM \ SOFTWARE”\ Classes \ VBSFile \ Shell \ Open \ Command的默认值中将“system32”更改为“sysWOW64”