Powershell如何在COM对象上查询接口 [英] How do Powershell query interface on a COM object

查看:1023
本文介绍了Powershell如何在COM对象上查询接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Powershell创建了一个COM对象:

I created a COM object using Powershell:

$obj = new-object -com MyLib.MyObj



<

Then I need to query the interface "MyLib.MyInterface" on that object, but I have no idea how to do it with PowerShell.

按顺序字
假设我有下面的C ++代码

In order word suppose I have the below C++ code

CComPtr<IInterface1> pInterface1;
CComPtr<IInterface2> pInterface2;
pInterface1->CoCreateInstance(CLSID_XXXX);   //in PowerShell: $obj = new-object -com MyLib.MyObj
pInterface1->QueryInterface(IID_YYYY, &pInterface2); //how to do this in PowerShell?

如何使用Powershell做同样的工作

How do I do the same job with Powershell

任何意见?

感谢

推荐答案

我创建了 $ obj = new-object -com文件。 (file是FileMoniker COM类的progid)。 [Runtime.InteropServices.marshal] :: GetIUnknownForObject($ obj)在我的Windows 2008R2上给我一个 System.IntPtr 机。我能够传递那个值,连同IMoniker的GUID到[Runtime.InteropServices.marshal] :: QueryInterface和我回来了相同的值(即相同的指针),因为我从GetIUnknownForObject。所以我可以查询界面。

As an experiment I created $obj=new-object -com file. ("file" is the progid for the FileMoniker COM class). [Runtime.InteropServices.marshal]::GetIUnknownForObject($obj) gives me an System.IntPtr on my Windows 2008R2 machine. I was able to pass that value, along with the GUID for IMoniker to [Runtime.InteropServices.marshal]::QueryInterface and I got back the same value (ie same pointer) as I got from GetIUnknownForObject. So I was able to query the interface.

但是,我不知道Powershell有什么好处。在[Runtime.InteropServices.marshal]中有很多其他方法可能对处理来自PS的COM感兴趣。但是一般来说,处理COM中的COM对象与处理C ++中的COM对象非常不同。

However, I'm not sure what good that does from Powershell. There are a lot of other methods in [Runtime.InteropServices.marshal] that might be of interest for dealing with COM from PS. But in general dealing with COM objects in PS is very different than dealing with them in C++.

编辑 b $ b我最近发现并验证了一种从PS访问一些 COM组件的方法,这可能在这里感兴趣。 Windows SDK附带了一组大量的IDL文件。如果要访问其中的一个(并且组件不实现IDispatch),可以使用MIDL编译IDL,然后使用TLBIMP创建互操作程序集。我成功地使用3个VSS硬件提供程序接口。

EDIT I recently found and verified a way to access some COM components from PS that might be of interest here. The Windows SDK comes with a large set of IDL files. If you want to access one of these (and the component doesn't implement IDispatch), you can compile the IDL with MIDL and then use TLBIMP to create an interop assembly. I successfully did this with the 3 VSS Hardware Provider interfaces.

我也学到了可以使用[type] :: GetTypeFromCLSID从CLSID获取类型。根据组件,您可以实例化它。

I also learned that you can use [type]::GetTypeFromCLSID to get a type from a CLSID. And depending on the component you can then instantiate it.

这篇关于Powershell如何在COM对象上查询接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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