读取未设置默认值的密钥时,WshShell.RegRead失败(仅适用于WinXP) [英] WshShell.RegRead fails when reading key with default value not set (WinXP only)

查看:148
本文介绍了读取未设置默认值的密钥时,WshShell.RegRead失败(仅适用于WinXP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows脚本宿主(JScript).我的主要目标是知道注册表项是否存在.我正在尝试'RegRead'该密钥,如果它引发错误,则该密钥不存在(这就是文档说

I'm working with Windows Script Hosting (JScript). And my primary goal is to know if registry key exist. I'm trying to 'RegRead' this key and if it throws an error then key does not exist (that's how documentation says http://msdn.microsoft.com/en-us/library/x05fawxd(v=vs.84).aspx). Everything works fine in Win7, but when I test my code in WinXP it fails. The problem is that when you try to 'RegRead' key that has no default value set it will throw an error (only on WinXP, on Win7 it is ok). And the more disgusting point is that error numbers are equal when you try to read non existing key (Error message: Invalid root in registry key "HKEY_CURRENT_USER\Software\NonExistKey\". Error code: 80070002) and existing key that has no default value (Error message: Unable to open registry key "HKEY_CURRENT_USER\Software\" for reading. Error code: 80070002). Yeah, error messages are different, but I don't want to compare messages or use indexOf("invalid") or indexOf("unable") to distinguish one error from another, because in defferent windows locales will be different messages and words. This is my samle code, Just make a JS file with this content and run it.

var shell = new ActiveXObject('WScript.Shell');

try{
    var valueKeyExist = shell.RegRead('HKEY_CURRENT_USER\\Software\\');
}catch(e2) {
     WScript.Echo([e2.message, e2.number]);
}

if (e2 !== undefined) {
    try{
        var valueNonExistKey = shell.RegRead('HKEY_CURRENT_USER\\Software\\NonExistKey\\');
    }catch(e1) {
        WScript.Echo([e1.message, e1.number]);
    }

    WScript.Echo('Seems like you are running this script on WinXP and reading reg key with default value not set throws an error.\nError messages are equal: ' + (e1.message == e2.message) + '\nError numbers are equal: ' + (e1.number == e2.number));
} else {
    WScript.Echo('Seems like you are running this script on Win7 and reading reg key with default value not set is ok');
}

关于如何解决此问题或如何知道注册表项的任何建议?

Any suggestions how to fix this or how can I know if registry key exist?

推荐答案

最终,我为此使用了WMI:

Eventually I used WMI for this:

function _getRootCode(root) {
    var rootCode = null;
    switch(root){
        case 'HKCR':
        case 'HKEY_CLASSES_ROOT':
            rootCode = 0x80000000;
            break;
        case 'HKCU':
        case 'HKEY_CURRENT_USER':
            rootCode = 0x80000001;
            break;
        case 'HKLM':
        case 'HKEY_LOCAL_MACHINE':
            rootCode = 0x80000002;
            break;
        case 'HKU':
        case 'HKEY_USERS':
            rootCode = 0x80000003;
            break;
        case 'HKCC':
        case 'HKEY_CURRENT_CONFIG':
            rootCode = 0x80000005;
            break;
    }
    return rootCode;
},

function keyExist(root, path) {
    var WMIRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv");
    var keys;
    return WMIRegistry.EnumKey(_getRootCode(root), path, keys) == 0;
}

在Win7和WinXP上运行正常.

Works fine on Win7 and WinXP.

这篇关于读取未设置默认值的密钥时,WshShell.RegRead失败(仅适用于WinXP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆