设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2032|回复: 0
打印 上一主题 下一主题

【原创】花花代码之:内存布局(开个头而己...)

[复制链接]
跳转到指定楼层
1#
发表于 2014-2-26 08:59:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
决定研究对象的内存布局,实现一个类似的SOS!
CLR2.0以上得靠自己琢磨了,所以暂没下文

  1. //-------------- 内存布局 ----------
  2. // written by 儒道佛 @2009.09
  3. // relation to pc@mye.cn
  4. //----------------------------------
  5. namespace PCTools.Memory
  6. {
  7.     using System;
  8.     using System.Runtime.InteropServices;
  9.     public interface IPointable
  10.     {
  11.         IntPtr Pointer { get; set; }
  12.         int Read(int ofs);
  13.         void Write(int ofs, int val);
  14.     }
  15.     public class PointerBase : IPointable
  16.     {
  17.         public virtual IntPtr Pointer { get; set; }
  18.         public virtual int PointerVal { get; set; }
  19.         
  20.         public virtual int Read(int ofs) { return Marshal.ReadInt32(this.Pointer, ofs); }
  21.         public virtual void Write(int ofs, int val) { Marshal.WriteInt32(this.Pointer, ofs, val); }
  22.     }
  23.     public static class Tools
  24.     {
  25.         //静态方法
  26.         public static void Show(IntPtr ptr, int ofs, int val, string tips)
  27.         {
  28.             string sVal = string.Format("{0:x8}", (uint)val);
  29.             int tmp;
  30.             tmp = (val & 0x000000FF) >> 0;
  31.             char c1 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
  32.             tmp = (val & 0x0000FF00) >> 8;
  33.             char c2 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
  34.             tmp = (val & 0x00FF0000) >> 16;
  35.             char c3 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
  36.             tmp = val >> 24;
  37.             char c4 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
  38.             Console.BackgroundColor = ConsoleColor.White;
  39.             Console.ForegroundColor = ConsoleColor.Gray;
  40.             Console.Write("0x{0:X8}  ", (uint)(ptr.ToInt32() + ofs));
  41.             Console.ForegroundColor = ConsoleColor.Black;
  42.             Console.Write("{0}{1} {2}{3} {4}{5} {6}{7}  ", sVal[6], sVal[7], sVal[4], sVal[5], sVal[2], sVal[3], sVal[0], sVal[1]);
  43.             Console.ForegroundColor = ConsoleColor.Black;
  44.             Console.Write("{0}{1}{2}{3}  ", c1, c2, c3, c4);
  45.             Console.ForegroundColor = ConsoleColor.Gray;
  46.             Console.WriteLine("{0,-30}", tips);
  47.             Console.ResetColor();
  48.         }
  49.         public static void Show(IntPtr ptr, int val, string tips)
  50.         {
  51.             Show(ptr, 0, val, tips);
  52.         }
  53.         public static void ShowArea(IntPtr ptr, int ofs, int len, string tips)
  54.         {
  55.             int curOfs = ofs;
  56.             int tmpVal;
  57.             string curTips;
  58.             Console.BackgroundColor = ConsoleColor.White;
  59.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  60.             Console.WriteLine("{0,-61}","-------------- " + tips + " --------------");
  61.             Console.ResetColor();
  62.             for (int i = 0; i < len; i++)
  63.             {
  64.                 curTips = string.Format("ofs:{0}", curOfs.ToString());
  65.                 tmpVal = Marshal.ReadInt32(ptr, curOfs);
  66.                 Show(ptr, curOfs, tmpVal, curTips);
  67.                 curOfs += 4;
  68.             }
  69.         }
  70.         public static void ShowArea(IntPtr ptr, int len, string tips)
  71.         {
  72.             ShowArea(ptr, 0, len, tips);
  73.         }
  74.         //扩展方法
  75.         public static void Show(this IPointable ptr, int ofs, string tips)
  76.         {
  77.             int val = Marshal.ReadInt32(ptr.Pointer, ofs);
  78.             Show(ptr.Pointer, ofs, val, tips);
  79.         }
  80.         public static void ShowArea(this IPointable ptr, int ofs, int len, string tips)
  81.         {
  82.             ShowArea(ptr.Pointer, ofs, len, tips);
  83.         }
  84.     }
  85.     //------------ ObjectInstance --------------
  86.     //  -04(04):SyncblkIndex
  87.     //  +00(04):MethodTable
  88.     //  +??(??):ComponentCount
  89.     //------------------------------------------
  90.     public class ObjectInstance : PointerBase, IPointable, IDisposable
  91.     {
  92.         private const int SyncblkIndex_Ofs = -4;
  93.         private const int TypeHandle_Ofs = 0;
  94.         //字段
  95.         private object _obj;
  96.         private GCHandle _gc;
  97.         private IntPtr _ptr_gc;
  98.         private IntPtr _ptr;
  99.         //属性
  100.         public override IntPtr Pointer
  101.         {
  102.             get { return _ptr; }
  103.             set { throw new Exception("non set"); }
  104.         }
  105.         public override int PointerVal
  106.         {
  107.             get { return Marshal.ReadInt32(Pointer); }
  108.             set { throw new Exception("non set"); }
  109.         }
  110.         public IntPtr GcPtr { get { return _ptr_gc; } }
  111.         public int GcVal { get { return Marshal.ReadInt32(GcPtr); } }
  112.         public IntPtr SyncblkIndexPtr { get { return new IntPtr(_ptr.ToInt32() + SyncblkIndex_Ofs); } }
  113.         public int SyncblkIndexVal { get { return Marshal.ReadInt32(SyncblkIndexPtr); } }
  114.         public IntPtr TypeHandlePtr { get { return new IntPtr(_ptr.ToInt32() + TypeHandle_Ofs); } }
  115.         public int TypeHandleVal { get { return Marshal.ReadInt32(TypeHandlePtr); } }
  116.         //构造
  117.         public ObjectInstance(object obj)
  118.         {
  119.             if (obj == null) throw new ArgumentNullException();
  120.             this._obj = obj;
  121.             this._gc = GCHandle.Alloc(obj);
  122.             this._ptr_gc = GCHandle.ToIntPtr(_gc);
  123.             this._ptr = Marshal.ReadIntPtr(_ptr_gc);
  124.         }
  125.         //诉构
  126.         ~ObjectInstance() { (this as IDisposable).Dispose(); }
  127.         void IDisposable.Dispose() { if (_gc.IsAllocated)_gc.Free(); }
  128.     }
  129.     //------------ MethodTable --------------
  130.     //  -12(12):GCInfo
  131.     //  +00(04):Flags
  132.     //  +04(04):Basic Instance Size
  133.     //  +08(04):EEClass
  134.     //  +12(04):Interface Vtable Map
  135.     //  +16(02):NumInterfaces
  136.     //  +18(02):CorElementType
  137.     //  +20(04):Module
  138.     //  +24(04):.cctor Slot
  139.     //  +26(02):Default .ctor Slot
  140.     //  +28(04):Interface Map
  141.     //  +32(04):Delegate
  142.     //  +36(04):Num Method Slots
  143.     //  +40(04):ToString...
  144.     //  +??(04):Equals...
  145.     //  .......:...........
  146.     //  +??(04):...........
  147.     //  +??(04):static string str
  148.     //  .......:...........
  149.     //  +??(04):Flags|Impl Start Slot...
  150.     //  +??(04):MyInterface1 TypeHandle...
  151.     //  +?2(04):Flags|Impl Start Slot...
  152.     //  +??(04):MyInterface2 TypeHandle...
  153.     //  +??(04):...........
  154.     //  +??(04):...........
  155.     //------------------------------------------
  156.     public class MethodTable : PointerBase, IPointable
  157.     {
  158.     }
  159. }
  160. namespace Run
  161. {
  162.     using System;
  163.     using PCTools.Memory;
  164.     interface IA
  165.     {
  166.         void Test();
  167.     }
  168.     class A : IA
  169.     {
  170.         private int i;
  171.         public int I
  172.         {
  173.             get { return i; }
  174.             set { i = value; }
  175.         }
  176.         public A() { this.i = 100; }
  177.         public A(int i) { this.i = i; }
  178.         public void Test()
  179.         {
  180.             Console.WriteLine(I);
  181.         }
  182.         class MyTest
  183.         {
  184.             static void Main()
  185.             {
  186.                 A a = new A();
  187.                 ObjectInstance objectInstance = new ObjectInstance(a);
  188.                 objectInstance.Show(0, "TypeHandle");
  189.                 objectInstance.Show(-4, "SyncblkIndex");
  190.                 Tools.Show(new IntPtr(0), a.GetHashCode(), "a's hashcode");
  191.                 objectInstance.Show(-4, "SyncblkIndex");
  192.                 Tools.ShowArea((IntPtr)objectInstance.TypeHandleVal, 20, "TypeHandle'0~20");
  193.                 Console.ReadKey();
  194.             }
  195.         }
  196.     }
  197. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 00:30 , Processed in 0.091970 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表