Invoking functions with `out` arguments, passing arguments by reference in JScript

一笑奈何 提交于 2020-01-04 09:07:31

问题


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);
};


来源:https://stackoverflow.com/questions/4307680/invoking-functions-with-out-arguments-passing-arguments-by-reference-in-jscri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!