Forum Discussion
HKosova
Alumni
12 years ago
Additionally, is it also safe to assume that the WMI service will be intstalled and running on any Window machine with .NET installed?
WMI is a built-in Windows component - it works even if .NET isn't installed.
I couldn't actually get the WMI method to work for me. Maybe you can see what I'm doing wrong. I managed to ge it to return a value, but the value always comes back as '2', instead of "0x00000409".
That's because GetStringValue returns the error code, not the value itself. The value is returned through the last out parameter.
Also, you forgot to double the backslash in the key name - "Control Panel\\International".
Here's the working example. It's a bit longer than the .NET method. :) I see you're already using the .NET method, but I thought I'd post it anyway.
function Test()
{
var HKCU = 0x80000001;
var key = "Control Panel\\International";
var value = "Locale";
var str = ReadRegStr(HKCU, strKey, strValue);
Log.Message(str);
}
// ----------------------------------------------------------------------------------------
// Returns a string value (REG_SZ) from the registry.
// Parameters:
// RootKey - one of the following values:
// HKEY_CLASSES_ROOT: 0x80000000
// HKEY_CURRENT_USER: 0x80000001
// HKEY_LOCAL_MACHINE: 0x80000002
// HKEY_USERS: 0x80000003
// HKEY_CURRENT_CONFIG: 0x80000005
// KeyName - the key name
// ValueName - the value name
// Architecture - the number 32 or 64 that indicates the registry bitness. Default is 32.
// ----------------------------------------------------------------------------------------
function ReadRegStr(RootKey, KeyName, ValueName, Architecture)
{
var oCtx = new ActiveXObject("WbemScripting.SWbemNamedValueSet");
if (typeof Architecture === 'undefined') Architecture = 32;
oCtx.Add("__ProviderArchitecture", Architecture);
var oLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var oWMI = oLocator.ConnectServer("", "root\\default", "", "", "", "", 0, oCtx);
var oReg = oWMI.Get("StdRegProv");
var oInParams = oReg.Methods_("GetStringValue").Inparameters;
oInParams.Hdefkey = RootKey;
oInParams.Ssubkeyname = KeyName;
oInParams.Svaluename = ValueName;
var oOutParams = oReg.ExecMethod_("GetStringValue", oInParams, 0, oCtx);
return oOutParams.SValue;
}