WScript.Shell RegWrite在Windows 8.1上失败

时间:2014-12-22 12:00:06

标签: javascript windows activex hta

我目前使用以下代码修改从我的.hta安装程序文件调用的JavaScript中的注册表。

var wsh = new ActiveXObject("WScript.Shell");
wsh.RegWrite("HKEY_LOCAL_MACHINE\\Software\\blah\\blah\\myKey", "0","REG_DWORD");

但是在Windows 8.1上,此代码失败,并显示Invalid root in registry key错误。

我已经对此进行了研究,但由于缺少权限,它似乎失败了,我需要做些什么才能确保此ActiveX调用以提升的权限运行?

1 个答案:

答案 0 :(得分:1)

你可以使用一个VBScript函数来测试用户是否有权限,这样(我不认为这可以在Javascript中使用,但它并不重要,因为Javascript可以使用在VBScript中声明的函数) :

<script type="text/vbscript">
     Function test()
          If CBool(IsNTAdmin) = True Then
               test = true      'Returns true if the user has elevated permissions
          Else
               test = false     'Returns false if the user doesn't
          End If
     End Function
</script>
<script type="text/javascript">
     if(test()){
          var wsh = new ActiveXObject("WScript.Shell");
          wsh.RegWrite("HKEY_LOCAL_MACHINE\\Software\\blah\\blah\\myKey","0","REG_DWORD");
     }
     else{
          alert("You don't have permission to do this!");
     }
</script>