OCX / COM:某些方法未显示在PowerShell中

时间:2011-03-17 13:01:09

标签: com powershell ocx

我有ActiveX控件SetACL.ocx。它已在我的系统上注册,我可以在C#(Visual Studio 2010)中使用它而不会出现问题。 Visual Studio的对象浏览器显示所有方法。

但是,在PowerShell中,大约三分之一的方法不会显示。如果我尝试调用其中一个缺少的方法,我会得到MethodNotFound:

PS C:\> $setacl = New-Object -ComObject setacl.setaclctrl.1
PS C:\> $setacl | Get-Member

   TypeName: System.__ComObject#{85869435-0ee3-440b-bf69-6c52c6638073}

Name                   MemberType Definition
----                   ---------- ----------
AddACE                 Method     int AddACE (string, bool, string, int, bool, int, int)
AddDomain              Method     int AddDomain (string, string, int, bool, bool)
AddTrustee             Method     int AddTrustee (string, string, bool, bool, int, bool, bool)
GetLastAPIError        Method     int GetLastAPIError ()
GetLastAPIErrorMessage Method     string ()
GetLastListOutput      Method     string GetLastListOutput ()
GetResourceString      Method     string GetResourceString (int)
Reset                  Method     void Reset ()
Run                    Method     int Run ()
SetAction              Method     int SetAction (int)
SetListOptions         Method     int SetListOptions (int, int, bool, int)
SetLogFile             Method     int SetLogFile (string)
SetOwner               Method     int SetOwner (string, bool)
SetPrimaryGroup        Method     int SetPrimaryGroup (string, bool)


PS C:\> $setacl.SetObject("test", 1)
Method invocation failed because [System.__ComObject#{85869435-0ee3-440b-bf69-6c52c6638073}] doesn't contain a method named 'SetObject'.
At line:1 char:18
+ $setacl.SetObject <<<< ("test", 1)
    + CategoryInfo          : InvalidOperation: (SetObject:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

关于为什么PowerShell没有显示所有可用方法但是Visual Studio的对象浏览器的任何线索?

我尝试了32位和64位版本的PowerShell。

更新1:

以下是ODL文件中缺少某个方法的签名(是的,我有OCX的完整源代码,我是它的作者):

[id(2), helpstring ("Set the object on which all actions are to be performed")]
LONG SetObject(BSTR sObjectPath, LONG nObjectType);

更新2:

以下是source code of the OCX,可在sourceforge上直接浏览。

更新3:

OCX可以是downloaded from sourceforge。它包含在文件SetACL 2.2.0.zip中。

更新4 - 可能的解决方案:

使用DISPID 1-7将方法更改为更高的DISPIDS并使用DISPIDS 1-7引入7种新的虚拟方法似乎可以解决问题。现在一切都出现在PoSh中 - 除了虚拟方法。

有没有人对此有解释?

6 个答案:

答案 0 :(得分:1)

查看项目here中的IDL文件,似乎忽略了DISPID低于8的_DSetACL接口的每个成员。也许尝试以更高的索引启动DISPID,比如100?我确实记得一些“神奇的”dispid值,但我认为它们是非常高的值,而不是低值,可能是错误的......

答案 1 :(得分:0)

尝试将-force参数添加到Get-Member?

总猜猜BTW ......

答案 2 :(得分:0)

尝试Get-Member -Static?再猜一遍。

答案 3 :(得分:0)

黑暗中的另一次刺伤......

Get-Member -Force -View All

还要确保获得正确版本的对象。

答案 4 :(得分:0)

当没有类型库时,通常会发生这种情况。当发生这种情况时,你通常会尝试这样的事情:

$bindingFlags = [System.Reflection.BindingFlags]::InvokeMethod
[System.__ComObject].InvokeMember('SetObject', $bindingFlags , $null, $setacl, 
                                  @("test", 1))

答案 5 :(得分:0)

这看起来像PowerShell特定的限制。提到了in the blog here两种解决方法。