Skip to content

Instantly share code, notes, and snippets.

@bopin2020
Last active June 8, 2023 19:21
Show Gist options
  • Save bopin2020/277e1432d277bc28356ff0292358c00a to your computer and use it in GitHub Desktop.
Save bopin2020/277e1432d277bc28356ff0292358c00a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
/*
0:000> !dumpmt -MD 00007ffdb57f5a10
EEClass: 00007ffdb57f2640
Module: 00007ffdb57f4140
Name: InterfaceAddress.IA
mdToken: 0000000002000002
File: D:\Tmp\InterfaceAddress\bin\Debug\InterfaceAddress.exe
BaseSize: 0x0
ComponentSize: 0x0
Slots in VTable: 1
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
Entry MethodDesc JIT Name
00007ffdb5900480 00007ffdb57f5a00 NONE InterfaceAddress.IA.DoSomethingA()
0:000> !dumpmt -MD 00007ffdb57f5aa8
EEClass: 00007ffdb57f26c8
Module: 00007ffdb57f4140
Name: InterfaceAddress.IB
mdToken: 0000000002000003
File: D:\Tmp\InterfaceAddress\bin\Debug\InterfaceAddress.exe
BaseSize: 0x0
ComponentSize: 0x0
Slots in VTable: 1
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
Entry MethodDesc JIT Name
00007ffdb5900490 00007ffdb57f5a98 NONE InterfaceAddress.IB.DoSomethingB()
0:000> !dumpmt -MD 00007ffdb57f5b78
EEClass: 00007ffdb57f25c8
Module: 00007ffdb57f4140
Name: InterfaceAddress.Program
mdToken: 0000000002000004
File: D:\Tmp\InterfaceAddress\bin\Debug\InterfaceAddress.exe
BaseSize: 0x18
ComponentSize: 0x0
Slots in VTable: 10
Number of IFaces in IFaceMap: 2
--------------------------------------
MethodDesc Table
Entry MethodDesc JIT Name
00007ffe128047a0 00007ffe1231c7a0 PreJIT System.Object.ToString()
00007ffe1287c070 00007ffe124b1a50 PreJIT System.Object.Equals(System.Object)
00007ffe12869960 00007ffe124b1a78 PreJIT System.Object.GetHashCode()
00007ffe12879880 00007ffe124b1a80 PreJIT System.Object.Finalize()
00007ffdb5900c80 00007ffdb57f5b60 JIT InterfaceAddress.Program.DoSomethingA()
00007ffdb5900ef0 00007ffdb57f5b68 JIT InterfaceAddress.Program.DoSomethingB()
00007ffdb5900c30 00007ffdb57f5b70 JIT InterfaceAddress.Program..ctor()
00007ffdb5901160 00007ffdb57f5b30 JIT InterfaceAddress.Program.CallA(InterfaceAddress.IA)
00007ffdb5901210 00007ffdb57f5b40 JIT InterfaceAddress.Program.CallB(InterfaceAddress.IB)
00007ffdb5900890 00007ffdb57f5b50 JIT InterfaceAddress.Program.Main(System.String[])
*/
namespace InterfaceAddress
{
interface IA
{
void DoSomethingA();
}
interface IB
{
void DoSomethingB();
}
internal class Program : IA, IB
{
private static void CallA(IA item)
{
IntPtr objaddr = Marshal.GetIUnknownForObject(item);
Console.WriteLine("CallA current obj: " + objaddr.ToString("x"));
}
private static void CallB(IB item)
{
IntPtr objaddr = Marshal.GetIUnknownForObject(item);
Console.WriteLine("CallB current obj: " + objaddr.ToString("x"));
}
static void Main(string[] args)
{
Program program = new Program();
program.DoSomethingA();
program.DoSomethingB();
IntPtr addr = Marshal.GetIUnknownForObject(program);
Console.WriteLine("currnet obj: " + addr.ToString("x"));
IA ia = new Program();
IntPtr iaaddr = Marshal.GetIUnknownForObject(ia);
Console.WriteLine("iaaddr obj: " + iaaddr.ToString("x"));
IB ib = new Program();
IntPtr ibaddr = Marshal.GetIUnknownForObject(ib);
Console.WriteLine("ibaddr obj: " + ibaddr.ToString("x"));
CallA(program);
CallB(program);
CallA(ia);
CallB(ib);
IntPtr addr1 = GCHandle.Alloc(program, GCHandleType.Pinned).AddrOfPinnedObject();
Console.WriteLine("addr1: " + addr1.ToString("x"));
IntPtr addr2 = GCHandle.Alloc(program, GCHandleType.Pinned).AddrOfPinnedObject();
Console.WriteLine("addr2: " + addr2.ToString("x"));
Console.ReadLine();
}
public void DoSomethingA()
{
IntPtr methodaddr = MethodBase.GetCurrentMethod().MethodHandle.GetFunctionPointer();
Console.WriteLine("A method: " + methodaddr.ToString("x"));
IntPtr objaddr = Marshal.GetIUnknownForObject(this);
Console.WriteLine("current obj: " + objaddr.ToString("x"));
IA ia = this as IA;
IntPtr iaaddr = Marshal.GetIUnknownForObject(ia);
Console.WriteLine("iaaddr obj: " + iaaddr.ToString("x"));
IB ib = this as IB;
IntPtr ibaddr = Marshal.GetIUnknownForObject(ib);
Console.WriteLine("ibaddr obj: " + ibaddr.ToString("x"));
Console.WriteLine();
}
public void DoSomethingB()
{
IntPtr methodaddr = MethodBase.GetCurrentMethod().MethodHandle.GetFunctionPointer();
Console.WriteLine("B method: " + methodaddr.ToString("x"));
IntPtr objaddr = Marshal.GetIUnknownForObject(this);
Console.WriteLine("current obj: " + objaddr.ToString("x"));
IA ia = this as IA;
IntPtr iaaddr = Marshal.GetIUnknownForObject(ia);
Console.WriteLine("iaaddr obj: " + iaaddr.ToString("x"));
IB ib = this as IB;
IntPtr ibaddr = Marshal.GetIUnknownForObject(ib);
Console.WriteLine("ibaddr obj: " + ibaddr.ToString("x"));
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment