Invoking functions with `out` arguments, passing a

2019-07-15 07:19发布

问题:

I'm using following code in JScript (WSH) to connect to local registry using WMI: var registry = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv'); and that works.

Then I have to determine if I'm allowed to delete key without really trying to delete it (e.g. perform a non-destructive check). I looked over docs and found that I need StdRegProv.CheckAccess() method. Problem is that CheckAccess returns result as out argument and I could not find VBScript's ByRef equivalent in JScript.

Somewhere in the Internet I've found that using SWbemServices.ExecMethod would help somehow, but I hadn't figured out how can I use that yet.

Could anyone provide me with code sample in JScript performing function call with argument passed by reference?

回答1:

Heh, got it working.

For anyone who will need it, CheckAccess invokation in JScript looks something like this:

function CheckAccess(defKey, subkeyName, required) {
    var providerName = "StdRegProv";
    var funcName = "CheckAccess";

    // connect to WMI
    var services = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default");

    // get provider
    var registry = services.Get(providerName);

    var in_params = registry.Methods_(funcName).InParameters.SpawnInstance_();
    in_params.hDefKey = defKey;
    in_params.sSubKeyName = subkeyName;
    in_params.uRequired = required;

    var outParams = services.ExecMethod(providerName, funcName, inParams);

    return Boolean(outParams.bGranted);
};