【问题标题】:Why does this VBScript give me an error?为什么这个 VBScript 给我一个错误?
【发布时间】:2009-04-21 19:58:17
【问题描述】:

我将这个 VBScript 脚本保存到我的本地计算机中,为 c:\test.vbs:

WScript.StdOut.WriteLine "This is a test"

当我从命令行运行它时,我得到了这个错误:

---------------------------
Windows Script Host
---------------------------
Script: C:\test.vbs
Line:   1
Char:   1
Error:  The handle is invalid. 
Code:   80070006
Source:     (null)

---------------------------
OK   
---------------------------

我在 Windows Vista (SP1) 和 Windows XP Pro (SP3) 下得到这个。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    此链接可能对您有所帮助:

    http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2004-07/0979.html

    看来StdOut 句柄仅在使用控制台主机 (cscript.exe) 而不是窗口主机 (wscript.exe) 时可用。如果你想让代码工作,你必须使用cscript.exe 来运行它。

    这篇文章还描述了如何更改默认行为以使用 cscript 而不是 wscript 运行脚本。

    【讨论】:

      【解决方案2】:

      如已接受答案中的文章所述,当我从命令提示符处调用我的脚本时,我的脚本工作正常:

      cscript test.vbs
      

      您还可以更改默认脚本主机,这样就不必每次都调用 cscript。之后,原始命令不会被修改。

      cscript //h:cscript //s 
      

      您可以通过以下方式恢复原始行为:

      cscript //h:wscript //s 
      

      谢谢!!

      【讨论】:

        【解决方案3】:

        我在错误“cscript - print output on same line on console?”中提交了这个解决方案,我认为这与这个问题有关。

        我在我的 JavaScript 中使用以下“日志”函数来支持 wscript 或 cscript 环境。正如你所看到的,这个函数只有在可以的情况下才会写入标准输出。

        var ExampleApp = {
            // Log output to console if available.
            //      NOTE: Script file has to be executed using "cscript.exe" for this to work.
            log: function (text) {
                try {
                    // Test if stdout is working.
                    WScript.stdout.WriteLine(text);
                    // stdout is working, reset this function to always output to stdout.
                    this.log = function (text) { WScript.stdout.WriteLine(text); };
                } catch (er) {
                    // stdout is not working, reset this function to do nothing.
                    this.log = function () { };
                }
            },
            Main: function () {
                this.log("Hello world.");
                this.log("Life is good.");
            }
        };
        
        ExampleApp.Main();
        

        【讨论】:

        猜你喜欢
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 2011-05-31
        • 2014-07-31
        • 1970-01-01
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        相关资源
        最近更新 更多