Question

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

My batch file code

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

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

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

Was it helpful?

Solution

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

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 method can not be used. It can close the hta, but will give no return value.

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.

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
)

OTHER TIPS

@echo OFF
echo Configuring Test...
start /wait "" AppConfigurationEditor.hta
echo %errorlevel%
if %errorlevel%==0 call TestConfigurationEditor.hta

should get the job done - provided errorlevel is returned as 0 for terminated normally and something else for 'cancelled'.

The echo %errorlevel% line is simply there for testing, just showing you what's going on.

start /wait will start the target, and pause the batch until the process finishes. The extra pair of rabbit's ears actually enclose the new window title - redundant here, but best to retain as start otherwise picks the first quoted string from the target (if any) as a window title.

You have to return the result of the pressed button as Exit Code that can be recuperated in the errorlevel, with Wscript.Quit [Value] in your two Hta code (or in the first if you just want to check the first)

Exemple :

Test.vbs

result=Msgbox("Are you sure?",vbokcancel, "")
WScript.Quit result

runtest.bat

@echo off&cls
cscript Test.vbs
if %errorlevel% Equ 1 Echo Process OK
if %errorlevel% Equ 2 Echo Process canceled
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top