Powershell 中的 New

编程入门 行业动态 更新时间:2024-10-26 20:23:32
本文介绍了Powershell 中的 New-Object -ComObject 和 VB 中的 CreateObject() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于 Windows PowerShell 在处理 Com Interop 时如何工作的问题.

我有一个 3rd 方应用程序(我们称之为 ThirdPartyApp),它公开了一个我可以调用的 API.

我可以在例如早期绑定它Excel 或 Visual Studio 并直接"与之对话.我可以在 VBScript 中后期绑定它,并且仍然可以直接"与它对话.

直接"是指我可以使用类似 ThirdPartyApp.Name

的语法调用 API 公开的属性和方法的方式

所以在 VBScript 中我可以做到:

Dim api : Set api = CreateObject("ThirdPartyApp.API")WScript.Echo api.Name

在 PowerShell 中,当我这样做时:

$api = New-Object -ComObject ThirdPartyApp.API

我必须使用这种语法来获取名称:

[System.__ComObject].InvokeMember('Name',[System.Reflection.BindingFlags]::GetProperty,$null,$api,$null)

现在,我明白这与 ThirdPartyApp 的构建方式有关.

我知道这不是编码问题,我希望我不会立即被否决,但我的问题是:为什么我在使用 Powershell 时不能以 VBScript 的方式与这个程序集对话?Powershell 中的 New-Object -ComObject 与 VBScript 中的 CreateObject(identifier) 有何不同?

解决方案

Visual Basic 程序员信息

Visual Basic 完全支持自动化.下表列出了 Visual Basic 语句如何转换为 OLE API.

Visual Basic 语句 OLE API

创建对象(ProgID")

  • CLSIDFromProgID

  • CoCreateInstance

  • QueryInterface 获取 IDispatch 接口.

GetObject(文件名"、ProgID")

  • CLSIDFromProgID

  • CoCreateInstance

  • IPersistFile 接口的查询接口.

  • 在 IPersistFile 接口上加载.

  • QueryInterface 获取 IDispatch 接口.

GetObject(文件名")\

  • CreateBindCtx 为后续函数创建绑定上下文.

  • MkParseDisplayName 返回 BindMoniker 的名字对象句柄.

  • BindMoniker 返回一个指向 IDispatch 接口的指针.

  • 释放名字句柄.

  • 根据上下文发布.

GetObject(ProgID")

  • CLSIDFromProgID

  • 在类 ID 上获取活动对象.

  • QueryInterface 获取 IDispatch 接口.

Dim x As New 界面

  • 查找接口的 CLSID.

  • CoCreateInstance

  • 查询接口

微软公司 2001 年 10 月 MSDN 库

请参阅 docs.microsoft/en-us/windows/win32/api/combaseapi/

I have a question about how Windows PowerShell works when dealing with Com Interop.

I have a 3rd party application (let's call it ThirdPartyApp) that exposes an API that I can call into.

I can early-bind it in e.g. Excel or Visual Studio and talk to it 'directly'. I can late-bind it in VBScript and still talk to it 'directly'.

By 'directly' I mean the way I can call properties and methods exposed by the API using syntax like ThirdPartyApp.Name

So in VBScript I can do:

Dim api : Set api = CreateObject("ThirdPartyApp.API") WScript.Echo api.Name

In PowerShell, when I do:

$api = New-Object -ComObject ThirdPartyApp.API

I have to use this syntax to get the Name:

[System.__ComObject].InvokeMember('Name',[System.Reflection.BindingFlags]::GetProperty,$null,$api,$null)

Now, I understand that that has something to do with how ThirdPartyApp was built.

I know this isn't a coding problem, and I hope I don't get downvoted immediately, but my question is: why can't I talk to this assembly the VBScript way when using Powershell? How does New-Object -ComObject in Powershell differ from CreateObject(identifier) in VBScript?

解决方案

Information for Visual Basic Programmers

Visual Basic provides full support for Automation. The following table lists how Visual Basic statements translate into OLE APIs.

Visual Basic statement OLE APIs

CreateObject ("ProgID")

  • CLSIDFromProgID

  • CoCreateInstance

  • QueryInterface to get IDispatch interface.

GetObject ("filename", "ProgID")

  • CLSIDFromProgID

  • CoCreateInstance

  • QueryInterface for IPersistFile interface.

  • Load on IPersistFile interface.

  • QueryInterface to get IDispatch interface.

GetObject ("filename")\

  • CreateBindCtx creates the bind context for the subsequent functions.

  • MkParseDisplayName returns a moniker handle for BindMoniker.

  • BindMoniker returns a pointer to the IDispatch interface.

  • Release on moniker handle.

  • Release on context.

GetObject ("ProgID")

  • CLSIDFromProgID

  • GetActiveObject on class ID.

  • QueryInterface to get IDispatch interface.

Dim x As New interface

  • Find CLSID for interface.

  • CoCreateInstance

  • QueryInterface

MSDN Library October 2001 Microsoft Corp.

See docs.microsoft/en-us/windows/win32/api/combaseapi/

更多推荐

Powershell 中的 New

本文发布于:2023-11-07 04:13:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565549.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!