gpt4 book ai didi

powershell - 如何在 PowerShell 中从 mscoree.tlb 实例化 CorRuntimeHost?

转载 作者:行者123 更新时间:2023-12-02 23:47:16 29 4
gpt4 key购买 nike

我想从 PowerShell 枚举当前进程中的所有 AppDomain。该进程恰好是托管 StudioShell 的 Visual Studio。 .为此,我需要实例化 CorRuntimHost,它是 mscoree.tlb 的一部分,因此我可以调整 this C# code ..

我试图获取 CorRunTimeHost 的正确名称并将其传递给 New-Object -COMObject "objectName" .基于 this forum posting ,我搜索了注册表,我认为正确的名称是 CLRMetaData.CorRuntimeHost .然而,虽然 New-Object -ComObject 'CLRMetaData.CorRuntimeHost' -Strict确实返回一个对象,它只公开 COM 对象固有的方法。

基于 this stackoverflow question我试过[Activator]::CreateInstance() .但是,以下两个语句给我带来了与 New-Object 相同的问题,即我无法调用 ICorRuntimeHost::EnumDomains()方法。

$corRuntimeHost = [Activator]::CreateInstance([Type]::GetTypeFromProgID('CLRMetaData.CorRuntimeHost'));
$enumerator = $null;
$corRuntimeHost.EnumDomains([ref]$enumerator);

Method invocation failed because [System.__ComObject] doesn't contain a method named 'EnumDomains'.
At line:1 char:1
+ $corRuntimeHost.EnumDomains([ref]$enumerator)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

最佳答案

为了让它在 PowerShell 3.0 中运行,我最终不得不使用 AssemblyBuilder .下面是工作代码:

问题似乎是 mscoree.CorRuntimeHostClass 没有公共(public)构造函数在 .NET 4.0 中,但在 3.5 中。

我后来在带有 powershell 2.0 的 Windows 7 VM 上对此进行了测试,现在这段代码将在 PowerShell 2.0 和 3.0 中运行。

$tlbName = Split-Path -Parent ([AppDomain]::CurrentDomain.GetAssemblies() | Where { $_.Location -Match '\\mscorlib.dll$' }).Location 
$tlbName = Join-Path $tlbName 'mscoree.tlb';

$csharpString = @"
//adapted from here http://blog.semanticsworks.com/2008/04/enumerating-appdomains.html
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

public class ListProcessAppDomains
{

[DllImport( `"oleaut32.dll`", CharSet = CharSet.Unicode, PreserveSig = false )]
private static extern void LoadTypeLibEx
(String strTypeLibName, RegKind regKind,
[MarshalAs( UnmanagedType.Interface )] out Object typeLib);

private enum RegKind
{
Default = 0,
Register = 1,
None = 2
}

private class ConversionEventHandler : ITypeLibImporterNotifySink
{
public void ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
{
Console.Error.WriteLine("Kind: {0} Code: {1} Message");
}

public Assembly ResolveRef( object typeLib )
{
string stackTrace = System.Environment.StackTrace;
Console.WriteLine("ResolveRef ({0})", typeLib);
Console.WriteLine(stackTrace);
return null;
}
}

public static AssemblyBuilder LoadMsCoreeDll( ref Object typeLib ) {
ConversionEventHandler eventHandler = new ConversionEventHandler();
string assemblyName = "PoshComWrapper.dll";
LoadTypeLibEx( @"$($tlbName)", RegKind.None, out typeLib );
TypeLibConverter typeLibConverter = new TypeLibConverter();
return typeLibConverter.ConvertTypeLibToAssembly( typeLib, assemblyName, 0, eventHandler, null, null, null, null );
}
}
"@

# So we can run this scipt multiple times
try { [ListProcessAppDomains] } catch { Add-Type -TypeDefinition $csharpString }

function Get-AppDomain {
$typeLib = $null;
$assemblyBuilder = [ListProcessAppDomains]::LoadMsCoreeDll([ref] $typeLib)
$corRuntimeHostClass = $assemblyBuilder.CreateInstance('PoshComWrapper.CorRuntimeHostClass')
$enumHandle = [IntPtr]::Zero
$corRuntimeHostClass.EnumDomains([ref] $enumHandle);
$appDomain = $null;
do
{
$corRuntimeHostClass.NextDomain($enumHandle, [ref] $appDomain);
if ($appDomain -ne $null -and $appDomain.GetType() -eq [AppDomain]) { $appDomain; }
} while ($appDomain -ne $null)
}

Get-AppDomain

关于powershell - 如何在 PowerShell 中从 mscoree.tlb 实例化 CorRuntimeHost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16513608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com