在HTA文件中使用Javascript从Windows注册表读取/写入 [英] Using Javascript in HTA file to read/write from Windows registry
问题描述
我目前正在尝试编写Javascript,以便在HTA文件中从Windows注册表进行读写。以下是我用来编写的当前代码:
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 + '"');
}
}
不幸的是,当我检查regedit中的值时,它们仍然存在不变。我确保仔细检查注册表路径是否与我在javascript中完全相同。它没有返回错误,所以我假设路径是正确的。
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.
我也试图尝试
var oWSS = WScript.CreateObject("WScript.Shell");
如 msdn 页面,而不是
var oWSS = new ActiveXObject("WScript.Shell");
但这给了我更多的问题。
but that just gave me more problems.
任何帮助表示赞赏!谢谢!
Any help is appreciated! Thanks!
推荐答案
我根据您的代码编写了一个示例HTA HTML应用程序,包括函数writeinRegistry()和readFromRegistry()函数。它为注册表写了一个值并检索它。问题是它放在哪里。搜索完注册表后,我发现它在HKEY_CURRENT_USER \ VirtualStore \ MACHINE \ SOFTWARE \ WOW6432None \ CompanyName \ CompanyValues下。这是因为:
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:
- 我在64位Windows 7计算机上运行此操作但执行32位版本的MSHTA.exe(默认)
- 我是以普通用户身份运行而不是提升
所以,那么,我创建了一个Windows快捷方式C:\ Windows \ System32 \ MSHTA.exe TheNameOfMyScript.hta。为了确保我运行的是64位版本,然后我使用提升执行快捷方式(以管理员身份运行快捷方式)。执行此操作后,HKLM分支下的注册表项已更新。
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>
这篇关于在HTA文件中使用Javascript从Windows注册表读取/写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!