使用`out`参数调用函数,在JScript中通过引用传递参数 [英] Invoking functions with `out` arguments, passing arguments by reference in JScript
问题描述
我正在JScript(WSH)中使用以下代码通过WMI连接到本地注册表:var registry = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv');
,并且可以正常工作.
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.
然后,我必须确定是否允许我删除密钥而不真正尝试删除它(例如,执行无损检查).我查看了文档,发现我需要StdRegProv.CheckAccess()
方法.问题是CheckAccess
返回结果作为out
参数,而我在JScript中找不到VBScript的ByRef
等效项.
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.
在互联网上的某个地方,我发现使用SWbemServices.ExecMethod
会有所帮助,但是我还没有弄清楚如何使用它.
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.
有人可以在JScript中通过引用传递参数来执行函数调用吗?
Could anyone provide me with code sample in JScript performing function call with argument passed by reference?
推荐答案
嘿,它可以正常工作了.
Heh, got it working.
对于任何需要它的人,JScript中的CheckAccess
调用看起来像这样:
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);
};
这篇关于使用`out`参数调用函数,在JScript中通过引用传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!