Last active
November 26, 2018 08:18
-
-
Save jkotas/db2536c79dd61899781406af5316037f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
[Guid("00000002-0000-0000-C000-000000000046")] | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
interface IMalloc | |
{ | |
[PreserveSig] | |
IntPtr Alloc(IntPtr cb); | |
[PreserveSig] | |
IntPtr Realloc(IntPtr pv, IntPtr cb); | |
[PreserveSig] | |
void Free(IntPtr pv); | |
[PreserveSig] | |
IntPtr GetSize(IntPtr pv); | |
[PreserveSig] | |
int DidAlloc(IntPtr pv); | |
[PreserveSig] | |
void HeapMinimize(); | |
} | |
class Program | |
{ | |
[DllImport("ole32.dll")] | |
static extern int CoGetMalloc(int context, out IMalloc ppMalloc); | |
static void Main() | |
{ | |
Marshal.ThrowExceptionForHR(CoGetMalloc(1, out IMalloc pMalloc)); | |
IntPtr p = pMalloc.Alloc(new IntPtr(4)); | |
p = pMalloc.Realloc(p, new IntPtr(100)); | |
pMalloc.DidAlloc(p); | |
pMalloc.Free(p); | |
pMalloc.HeapMinimize(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.IO; | |
using System.Runtime.Loader; | |
class UnloadableAssemblyLoadContext : AssemblyLoadContext | |
{ | |
public UnloadableAssemblyLoadContext() : base(true) | |
{ | |
} | |
protected override Assembly Load(AssemblyName assemblyName) | |
{ | |
return null; | |
} | |
} | |
class My | |
{ | |
static void Work() | |
{ | |
var alc = new UnloadableAssemblyLoadContext(); | |
alc.LoadFromAssemblyPath(Path.GetFullPath(@"comtest\comtest.exe")).EntryPoint.Invoke(null, null); | |
} | |
static void Main() | |
{ | |
for (; ; ) | |
{ | |
Work(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment