如何从一个HTA(VBScript)的返回退出状态给调用批处理文件 [英] How to return exit status from an HTA (vbscript) to the calling Batch file

查看:450
本文介绍了如何从一个HTA(VBScript)的返回退出状态给调用批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个HTA(VBScript)的返回退出状态调用批处理文件?

How to return exit status from an HTA (vbscript) to the calling Batch file ?

我的批处理文件,code

My batch file code

@echo OFF
echo Configuring Test...
call AppConfigurationEditor.hta
call TestConfigurationEditor.hta

1.如果在取消的第一个HTA我不按钮用户点击想要运行第二HTA。

1.If user click on CANCEL button on the first HTA I dont want to run 2nd HTA.

2.Batch脚本调用/显示第二HTA立即,而不是等到第一个HTA的结束。

2.Batch script calls/displays the 2nd HTA immediately, not wait till closing of 1st HTA.

推荐答案

卫生技术评估有一个错误级别返回给调用者的过程中没有实现的方法。

HTAs have no implemented way of returning an errorlevel to the caller process.

WScript.Quit 不能使用。该lenguage发动机的VBScript / JavaScript的/ ...是一样的,但实例化引擎主机对象不是习惯性的Windows脚本宿主,它是一个浏览器对象,那么的WScript 对象不存在。

WScript.Quit can not be used. The lenguage engine vbscript/javascript/... is the same, but the host object that instantiates the engine is not the habitual windows scripting host, it is a browser object, so WScript object does not exist.

window.close 方法。它可以关闭HTA,但会给没有返回值。

window.close method can not be used. It can close the hta, but will give no return value.

使用HTA返回一个值到调用进程的常用方法是坚持一个文件中或在注册表中的此值。然后调用进程可以检索所需的值。

The usual way of using an hta to return a value to the calling process is to persist this value inside a file or in the registry. Then the calling process can retrieve the required values.

如果需要错误级别,没有直接的方法。但间接方法可以被实现。只需使用WMI来检索正在运行的进程列表中,找到当前的HTA,并且这个过程调用终止方法允许设置一个退出值。

If errorlevel is needed, there is no direct method. But an indirect method can be implemented. Just use WMI to retrieve the list of running processes, locate the current hta, and for this process call the Terminate method which allow to set an exit value.

<HTML>
    <HEAD>
        <HTA:APPLICATION 
            ID              = "testCloseHTA" 
            APPLICATIONNAME = "testCloseHTA"
            VERSION         = "0.1"
            NAVIGABLE       = "yes"
            SHOWINTASKBAR   = "yes" 
            SINGLEINSTANCE  = "yes" 
            WINDOWSTATE     = "normal"

            BORDER          = "normal" 
            BORDERSTYLE     = "normal"
            INNERBORDER     = "no" 

            CAPTION         = "yes" 
            MINIMIZEBUTTON  = "yes"
            MAXIMIZEBUTTON  = "yes"
            SYSMENU         = "yes" 
            SCROLL          = "yes" 
            SCROLLFLAT      = "yes"

            CONTEXTMENU     = "yes"
            SELECTION       = "yes"
        />

        <TITLE>testCloseHTA</TITLE>

        <STYLE>
            body { font-size: 1em; font-family:Verdana; }
        </STYLE>

        <SCRIPT language="Javascript">
            function closeWithErrorlevel(errorlevel){
                var colProcesses = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2').ExecQuery('Select * from Win32_Process Where Name = \'mshta.exe\'');
                var myPath = (''+location.pathname).toLowerCase();
                var enumProcesses = new Enumerator(colProcesses);
                for ( var process = null ; !enumProcesses.atEnd() ; enumProcesses.moveNext() ) {
                    process = enumProcesses.item();
                    if ( (''+process.CommandLine).toLowerCase().indexOf(myPath) > 0 ){
                        process.Terminate(errorlevel);
                    }
                }
            }

            function closeHTA(value){
                // test close of window. Use default value
                if (typeof value === 'undefined') value = 0; 
                try { closeWithErrorlevel(value) } catch (e) {};
            }

        </SCRIPT>

    </HEAD>

    <BODY>
        <button onclick="closeHTA(0);">close application 0</button>
        <button onclick="closeHTA(1);">close application 1</button>
        <button onclick="closeHTA(2);">close application 2</button>
    </BODY>

    <script language="Javascript">
            window.attachEvent('onbeforeunload',closeHTA);
    </script>

</HTML>

然后就可以从批处理文件调用它作为

Then you can call it from batch file as

start "" /wait testCloseHTA.hta
if errorlevel 2 (
    echo option 2 has been selected
) else if errorlevel 1 (
    echo option 1 has been selected
) else (
    echo option 0 has been selected
)

这篇关于如何从一个HTA(VBScript)的返回退出状态给调用批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆