8000 Add NativeHost.LoadAssemblyData helper function to allow hosting Powe… · awakecoding/PowerShell@ffac56c · GitHub
[go: up one dir, main page]

Skip to content

Commit ffac56c

Browse files
Marc-André MoreauSteveL-MSFT
Marc-André Moreau
authored andcommitted
Add NativeHost.LoadAssemblyData helper function to allow hosting PowerShell inside native processes 100% at runtime
1 parent 59715d5 commit ffac56c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.IO;
8+
using System.Runtime.Loader;
9+
using System.Runtime.InteropServices;
10+
11+
namespace Microsoft.PowerShell
12+
{
13+
/// <summary>
14+
/// Provides helper functions to faciliate calling managed code from a native PowerShell host.
15+
/// </summary>
16+
public static class NativeHost
17+
{
18+
/// <summary>
19+
/// Load an assembly in memory from unmanaged code.
20+
/// </summary>
21+
/// <param name="data">
22+
/// Unmanaged pointer to assembly data buffer
23+
/// </param>
24+
/// <param name="size">
25+
/// Size in bytes of the assembly data buffer
26+
/// </param>
27+
[UnmanagedCallersOnly]
28+
public static void LoadAssemblyData(IntPtr data, int size)
29+
{
30+
byte[] bytes = new byte[size];
31+
Marshal.Copy(data, bytes, 0, size);
32+
Stream stream = new MemoryStream(bytes);
33+
AssemblyLoadContext.Default.LoadFromStream(stream);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)
0