【发布时间】: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