在UAC下的ShellWindows中搜索

时间:2012-12-25 23:49:04

标签: javascript uac jscript wsh hta

我有一个JScript应用程序,它运行HTA窗口与用户通信。它启动具有特定ID的HTA应用程序,然后在shellWindows集合中搜索具有此ID的窗口并对其进行控制。 它在大多数情况下都能正常工作,但在Windows 7上使用UAC脚本需要提升其权限。当它这样做时,createHTAWindow函数不能正常工作,因为它无法在shellWindows集合中找到HTA窗口。如果脚本没有尝试提升其权限,那么HTA部分工作正常。 以下是重现问题的代码示例。它适用于Windows 7,UAC关闭或UAC开启,Elevate变量处于False状态。但是如果UAC打开并且Elevate == True,那么它不会:/

var WshShell = new ActiveXObject("WScript.Shell");
var Elevate = true;

function main(){
    var oXMLHTTP = new ActiveXObject("MSXML2.XMLHTTP");    
    innerhtml =
        "<html> " +
        "<head> " +
        "<title>Test HTA window</title> " +
        "<HTA:APPLICATION " +
        "APPLICATIONNAME='test' " +
        "VERSION='1.0' " +
        "BORDER='none' " +
        "CAPTION='yes' " +
        "INNERBORDER='no' " +
        "CONTEXTMENU='no' " +
        "SCROLL='no' " +
        "MAXIMIZEBUTTON='no' " +
        "MINIMIZEBUTTON='no' " +
        "SELECTION='no' " +
        "/> " +
        "<STYLE> " +
        "body{ " +
        "font-family:Verdana; " +
        "font-size:11px; " +
        "} " +
        "</STYLE> " +
        "</head> " +
        "<script language='javascript'> " +
        "function window.onload(){ " +
        "self.focus(); " +
        "self.resizeTo(300,80); " +
        "} " +
        "</script> " +
        "<body scroll=no bgcolor='D4D0C8' style='border:0;'> " +
        "<center> Test HTA window " +
        "<br><br> " +
        " It will be closed in 10 sec " +
        "</center> " +
        "</body> " +
        "</html> ";
    width = 300; height = 80;
    var window = createHTAWindow(innerhtml,0,0,width,height);
    window.moveTo((window.screen.width-width)/2,(window.screen.height-height)/2);
    WScript.Sleep(10000);
    window.close();
}
// function createHTAWindow by JSman
function createHTAWindow(content, x, y, width, height)
{
var HTASettings = "", e, window, Host = this; 
    width = Number(width) || 400;
    height = Number(height) || 300;
    try {
        HTASettings = content.match(/<hta[^>]+>/gim)[0].replace(/\r?\n/g, " ").replace(/"/g,"'");
    } catch(e) {
    }

    var ID = "u"+Math.floor(Math.random()*10000000)
    var CodeForLinking = "\"<title> </title><script>moveTo(-300,-300); resizeTo(0,0)</script>" + 
        HTASettings + 
        "<object id=" + ID + 
        " classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object>\"";
    var ShellWindows = ( new ActiveXObject("Shell.Application") ).Windows();
    var NumberOfWindows = ShellWindows.Count;

    WshShell.Run("mshta.exe about:"+CodeForLinking);

// for debug purpose
WScript.Sleep(1000);
var s="ID: " + ID + "\n";
for (var i=ShellWindows.Count; --i>=0;){
    s = s + i + ": " + ShellWindows.Item(i).id + "\n";
}
WScript.Echo(s);

    outer: for (var t=0; t<500; t++)    
    inner:  for (var i=ShellWindows.Count; --i>=0;){
        try{
            if( ShellWindows.Item(i).id == ID ){
                window = ShellWindows.Item(i).parent.parentWindow;
                break outer;
            }
        } catch(e) {
            WScript.Echo(e.message);
            return false;
        }
        WScript.Sleep(10);
    }

    if (!window) {
        WshShell.Popup("Error", 0, "Error", 0+48);
        WScript.Quit();
        return false;
    }

    try {
        window.document.open(); window.Host = Host; 
        window.document.write([content||"", "<script language='JScript'>eval;resizeTo(",width,",", height,"); moveTo(", Number(x) ||  (window.screen.width - width) / 2,",", Number(y) || (window.screen.height - height) / 2, ");</script>"].join(""));
        window.document.close();
    } catch (e) {
    }
    return window;
}

if ( Elevate && WScript.Arguments.Count() == 0 ) {
    var oShell = new ActiveXObject("Shell.Application");
    oShell.ShellExecute("wscript.exe", "\"" + WScript.ScriptFullName + "\"" + " /isElevated", WScript.ScriptFullName.slice(0,-WScript.ScriptName.length-1), "runas", 1);
    WScript.Quit();
}

main();

你能解释为什么会发生这种情况并提出解决这个问题的建议吗?

0 个答案:

没有答案