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 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:
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>