Using Javascript in HTA file to read/write from Windows registry

谁说我不能喝 提交于 2019-12-03 20:28:02

问题


I'm currently attempting to write Javascript in order to read and write from the Windows registry in an HTA file. Here is the current code I am using to write:

writeInRegistry = function (sRegEntry, sRegValue) {
    Regpath = "HKEY_LOCAL_MACHINE\\Software\\CompanyName\\CompanyValues\\" + sRegEntry;

    try {
        var oWSS = new ActiveXObject("WScript.Shell");

        oWSS.RegWrite(Regpath, sRegValue, "REG_DWORD");
        oWSS = null;


    } catch (e) {
        alert('Error trying to write "' + sRegValue + '" to registry entry "' + sRegEntry + '"');
    }
}

Unfortunately when I check the values in regedit, they remain unchanged. I made sure to double check that the registry path is exactly the same as I have it in javascript. It doesn't return an error, so I'm assuming the path is correct.

I also attempted to try

var oWSS = WScript.CreateObject("WScript.Shell");

as referred to in this msdn page, instead of

var oWSS = new ActiveXObject("WScript.Shell");

but that just gave me more problems.

Any help is appreciated! Thanks!


回答1:


I wrote a sample HTA HTML Application including functions writeinRegistry() and readFromRegistry() function based on your code. It wrote a value to the registry and retrieved it. The question is where did it place it. After searching the registry, I found it under HKEY_CURRENT_USER\VirtualStore\MACHINE\SOFTWARE\Wow6432None\CompanyName\CompanyValues. This is because:

  • I was running this on a 64-bit Windows 7 machine but executing the 32-bit version of MSHTA.exe (default)
  • I was running as a regular user not elevated

So, then, I created a Windows shortcut to C:\Windows\System32\MSHTA.exe TheNameOfMyScript.hta. To ensure I was running the 64-bit version and then I executed the shortcut with elevation (Run the shortcut as Administrator). After doing this, the registry key under the HKLM branch updated.

<html>
<head>
<title>RegTest</title>
<script language="JavaScript">
function writeInRegistry(sRegEntry, sRegValue)
{
  var regpath = "HKEY_LOCAL_MACHINE\\Software\\CompanyName\\CompanyValues\\" + sRegEntry;
  var oWSS = new ActiveXObject("WScript.Shell");
  oWSS.RegWrite(regpath, sRegValue, "REG_DWORD");
}

function readFromRegistry(sRegEntry)
{
  var regpath = "HKEY_LOCAL_MACHINE\\Software\\CompanyName\\CompanyValues\\" + sRegEntry;
  var oWSS = new ActiveXObject("WScript.Shell");
  return oWSS.RegRead(regpath);
}

function tst()
{
  writeInRegistry("Version", "101");
  alert(readFromRegistry("Version"));
}
</script>
</head>
<body>
Click here to run test: <input type="button" value="Run" onclick="tst()"
</body>
</html>



回答2:


I have found that when you update registry keys in code whilst regedit.exe is open/running it is a good idea to press F5 in regedit to refresh it after the code ran.



来源:https://stackoverflow.com/questions/8884347/using-javascript-in-hta-file-to-read-write-from-windows-registry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!