【问题标题】:Getting DTE for running Visual Studio instances from PowerShell从 PowerShell 获取用于运行 Visual Studio 实例的 DTE
【发布时间】:2020-07-21 04:59:43
【问题描述】:

使用 PowerShell 为单个正在运行的 Visual Studio 获取 DTE 非常简单: Get a handle on the running Visual Studio instance (DTE) from powershell

使用 C# 获取多个实例的 DTE 已解决 How do I get the DTE for running Visual Studio instance?

现在如何使用 PowerShell 获取多个 VS 实例的 DTE?我正在尝试启用脚本来调整 VS 配置并在 git pull、VM+XDE 设置等之后开始构建,如果已经打开了一个新的 VS 窗口,则所有这些都无需打开。

这是我得到的:

$memberDefinitionGetRunningObjectTable = @'
[DllImport(@"ole32.dll",EntryPoint="GetRunningObjectTable",ExactSpelling=false)] 
public static extern int GetRunningObjectTable(
    int reserved,
    out IRunningObjectTable prot);
'@
$typeGetRunningObjectTable = Add-Type -MemberDefinition $memberDefinitionGetRunningObjectTable -Name "InteropGetRunningObjectTable" -PassThru -Using 'System.Runtime.InteropServices.ComTypes'

[System.Runtime.InteropServices.ComTypes.IRunningObjectTable]$runningObjects = $null
$ret = $typeGetRunningObjectTable::GetRunningObjectTable(0, [ref][System.Runtime.InteropServices.ComTypes.IRunningObjectTable] $runningObjects)
if ($ret -eq 0)
{
    if ($null -eq $runningObjects) { Write-Error "Failed to retrieve running COM objects"}
}

-- 这确实失败了,所以似乎在out 参数上强制输入IRunningObjectTable 不起作用,而$runningObjects$null

我之前试过这个:

$memberDefinitionGetRunningObjectTable = @'
[DllImport(@"ole32.dll",EntryPoint="GetRunningObjectTable",ExactSpelling=false)] 
public static extern int GetRunningObjectTable(
    int reserved,
    out IRunningObjectTable prot);
'@
$typeGetRunningObjectTable = Add-Type -MemberDefinition $memberDefinitionGetRunningObjectTable -Name "InteropGetRunningObjectTable" -PassThru -Using 'System.Runtime.InteropServices.ComTypes'

$memberDefinitionCreateBindCtx = @'
[DllImport("ole32.dll")]
public static extern int CreateBindCtx(
    int reserved,
    out IBindCtx ppbc); 
'@
$typeCreateBindCtx = Add-Type -MemberDefinition $memberDefinitionCreateBindCtx -Name "InteropCreateBindCtx" -PassThru -Using 'System.Runtime.InteropServices.ComTypes'

$runningObjects = $null
$ret = $typeGetRunningObjectTable::GetRunningObjectTable(0, [ref] $runningObjects)
if ($ret -eq 0)
{
    if ($null -eq $runningObjects) { throw "Failed to retrieve running COM objects"}

    $enumMoniker = $null
    $runningObjects.EnumRunning([ref] $enumMoniker)

    $typeCreateBindCtx::CreateBindCtx([ref] $enumMoniker)
}

这会导致这个错误:

Method invocation failed because [System.__ComObject] does not contain a method named 'EnumRunning'.
At line:6 char:5
+     $runningObjects.EnumRunning([ref] $enumMoniker)

我只是有点难过。 __ComObject 实际上也是$null 吗?

【问题讨论】:

    标签: visual-studio powershell com envdte


    【解决方案1】:

    您向我发送了一个不同的答案,与此有些相关。我所能建议的就是看看我十年前是怎么做到的:

    https://github.com/Pscx/Pscx/blob/master/Src/Pscx.Core/Interop/RunningObjectTable/RunningObjectTableHelper.cs

    此代码最初在 CodePlex 上。

    因此,如果您运行“install-module psx”,您将免费获得已编写的 Get-RunningObjectTable。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2017-12-27
      • 2019-07-31
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多