PowerShell中的COM接口包装器? [英] COM interface wrappers in PowerShell?

查看:337
本文介绍了PowerShell中的COM接口包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有以下代码,我尝试移植到PowerShell。
但我不知道如何移植这个转换:

I have the following code in C# which I try to port to PowerShell. But I don't know how to port this cast:

((_ISkypeEvents_Event)skype).CallStatus += CallStatusHandler;

如果我在PowerShell控制台输入[Skype4COM.ISkypeEvents_Event],我会得到:

If I just type [Skype4COM.ISkypeEvents_Event] in my PowerShell console I get:


无法找到类型[Skype4COM.ISkypeEvents_Event]:请确保加载了包含此类型的程序集。

Unable to find type [Skype4COM.ISkypeEvents_Event]: make sure that the assembly containing this type is loaded.

但是,我可以获得$ skype对象的所有成员:

However, I can get all the members of the $skype object:

$skype = New-Object -ComObject Skype4COM.Skype

以下行不起作用:

$skypeevent = [Skype4COM._ISkypeEvents_Event]$skype

如果我尝试直接在$ skype对象上调用方法,像这样:

If I try to call the method directly on the $skype object, like this:

$skype.add_CallStatus({ write-host "yay" })

预期)告诉我:


方法调用失败,因为[System .__ ComObject#{b1878bfe-53d3-402e-8c86-190b19af70d5}] 't包含一个名为'add_CallStatus'的方法。

Method invocation failed because [System.__ComObject#{b1878bfe-53d3-402e-8c86-190b19af70d5}] doesn't contain a method named 'add_CallStatus'.

我试图创建一个COM包装器,类型的COM接口...

I've tried to create a COM wrapper, but it still fails on getting the type for the COM interface...

任何想法?非常感谢!

推荐答案

PowerShell特殊情况COM对象具有自己的后期绑定COM适配器调用者(以及 Get-Member cmdlet)。很遗憾,如果找不到相关的类型库,这通常会发生在实例实际上是通过transparentproxy类型出现的远程COM对象。

PowerShell special-cases COM objects with its own late-binding "COM Adapter" in order to expose members to callers (and the Get-Member cmdlet). Unfortunately this sometimes fails if it cannot find the associated type library, which usually happens when the instance is actually a remote COM object surfaced through a transparentproxy type.

此COM适配的另一个副作用是,您被间接阻止使用这些类型的转换来访问成员。 PowerShell通常暴露互操作程序集(动态创建或PIA)CoClass类,它包括所有接口的成员。事实上,对接口的这种限制不仅仅是与COM对象:PowerShell中的.NET适配器不处理纯旧的.NET接口。说实话,这是99%的案例的首选行为。 PowerShell是一种动态语言,将始终在运行时公开引用的真实类型。任何投射到接口的尝试都将被忽略。

Another side-effect of this COM adaptation is that you are indirectly prevented from using these kind of casts to gain access to members. PowerShell generally exposes the interop assembly's (dynamically created or PIA) CoClass class, which includes members of all interfaces. In fact, this limitation on interfaces is not just with COM objects: the ".NET Adapter" in PowerShell doesn't deal with plain old .NET interfaces either. To be honest, this is the preferred behaviour for 99% of cases. PowerShell is a dynamic language and will always expose the true type of a reference at runtime. Any attempts to cast to an interface will be ignored.

当你在C#中显式实现接口时,会导致更多的问题。 PowerShell根本看不到它们! 我有关于某种技术的博客来使用v2来代理显式接口成员.0模块。你可以尝试一个COM接口,但我不知道它会工作。

This leads to more problems when you get to explicitly implemented interfaces in C#. PowerShell can't see them at all! I did blog about a technique to proxy explicit interface members using a v2.0 module. You could try it against a COM interface, but I'm not sure it would work.

这篇关于PowerShell中的COM接口包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆