【.Net】使用 mscoree

概述:如何在代码中使用 mscoree

0x01 添加 mcsoree.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// MSCOREE.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace mscoree
{
[CompilerGenerated]
[Guid("CB2F6722-AB3A-11D2-9C40-00C04FA30A3E")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[TypeIdentifier]
[ComImport]
[CLSCompliant(false)]
public interface ICorRuntimeHost
{
void _VtblGap1_11();

void EnumDomains(out IntPtr enumHandle);

void NextDomain([In] IntPtr enumHandle, [MarshalAs(UnmanagedType.IUnknown)] out object appDomain);

void CloseEnum([In] IntPtr enumHandle);

}
}

0x02 添加导出函数

1
2
3
4
private static ICorRuntimeHost GetCorRuntimeHost()
{
return (ICorRuntimeHost)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E")));
}

0x03 使用

1
2
ICorRuntimeHost host = null;
host = GetCorRuntimeHost();

0x04 获取所有 AppDomains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static void GetAllAppDomains()
{

AppDomain one = AppDomain.CreateDomain("One");
AppDomain two = AppDomain.CreateDomain("Two");
// Creates 2 app domains

List<AppDomain> appDomains = new List<AppDomain>();

IntPtr enumHandle = IntPtr.Zero;

ICorRuntimeHost host = null;
host = GetCorRuntimeHost();

try
{

host.EnumDomains(out enumHandle);

object domain = null;

AppDomain tempDomain;

while (true)
{

host.NextDomain(enumHandle, out domain);

if (domain == null)
{
break;
}

tempDomain = domain as AppDomain;

appDomains.Add(tempDomain);

}

}

catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

finally
{
host.CloseEnum(enumHandle);
int rel = Marshal.ReleaseComObject(host);
}

Assembly[] assemblies;
foreach (AppDomain app in appDomains)
{
Console.WriteLine(app.FriendlyName);

assemblies = app.GetAssemblies();

Console.WriteLine("-----------------------Assemblies------------------");
foreach (Assembly assem in assemblies)
{
Console.WriteLine(assem.FullName);
}
Console.WriteLine("---------------------------------------------------");
}

}

【.Net】使用 mscoree
https://hodlyounger.github.io/B_Code/CSharp/【.Net】使用 mscoree/
作者
mingming
发布于
2024年7月2日
许可协议