最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using Javascript in HTA file to readwrite from Windows registry - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question asked Jan 16, 2012 at 18:08 JonJon 311 gold badge1 silver badge2 bronze badges 2
  • I proposed an alternate approach without testing your code first. However, after testing, I have pletely changed my original answer. – Abbas Commented Jan 16, 2012 at 22:50
  • I ran your code and it worked perfectly. Make sure you run the registry editor (regedit) as well as your HTA application as an Administrator, to observe the changes that your code made. – Abbas Commented Jan 27, 2012 at 5:19
Add a ment  | 

2 Answers 2

Reset to default 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>

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.

发布评论

评论列表(0)

  1. 暂无评论