How do Powershell query interface on a COM object
Asked Answered
I

3

10

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.

In other words 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?

How do I do the same job with Powershell?

Impenetrable answered 7/12, 2011 at 2:59 Comment(0)
S
5

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.

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++.

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.

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.

Scever answered 24/9, 2015 at 20:47 Comment(0)
M
4

If I understood your needs, try this:

$obj = new-object -com MyLib.MyObj

$type = $obj.gettype()

$type.GetInterfaces() # give a list of interfaces for the type

hope can be a starting point

Mandal answered 7/12, 2011 at 8:27 Comment(2)
Christian, thanks for your answer! however, I tried following your instruction but get the error saying the object doesn't contain a method named GetTypeImpenetrable
in your sample code, $obj is my COM object. but I dont know how to cast it to other COM interface (c++ code: QueryInterface(obj,....))Impenetrable
C
1

Here is an example where I call Word (see Word Object Model Overview) COM object :

# Create Word Object  
$wrd = new-object -com "word.application"

# Make Word Visible  
$wrd.visible = $true

# Open a document   
$doc = $wrd.documents.open("C:\silogix\silogix.doc")

To see properties and methods of your COM object you can use :

$obj | Get-Member
Caffeine answered 7/12, 2011 at 4:38 Comment(1)
Well, this is the code of creating a CoClass. I am wondering how to Query the interfaces on the object. Normally, with C++ the code looks like: obj->QueryInterface(ID_MyInterface, &obj2). With C# the code looks like: obj2 = obj as MyLib.MyInterface; But I do not have idea of how to do this with PowershellImpenetrable

© 2022 - 2024 — McMap. All rights reserved.