Skip to content

Instantly share code, notes, and snippets.

@jkotas
Last active November 26, 2018 08:18
Show Gist options
  • Save jkotas/db2536c79dd61899781406af5316037f to your computer and use it in GitHub Desktop.
Save jkotas/db2536c79dd61899781406af5316037f to your computer and use it in GitHub Desktop.
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();
}
}
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