Office中国论坛/Access中国论坛
标题:
【原创】花花代码之:内存布局(开个头而己...)
[打印本页]
作者:
faunus
时间:
2014-2-26 08:59
标题:
【原创】花花代码之:内存布局(开个头而己...)
决定研究对象的内存布局,实现一个类似的SOS!
CLR2.0以上得靠自己琢磨了,所以暂没下文
//-------------- 内存布局 ----------
// written by 儒道佛 @2009.09
// relation to pc@mye.cn
//----------------------------------
namespace PCTools.Memory
{
using System;
using System.Runtime.InteropServices;
public interface IPointable
{
IntPtr Pointer { get; set; }
int Read(int ofs);
void Write(int ofs, int val);
}
public class PointerBase : IPointable
{
public virtual IntPtr Pointer { get; set; }
public virtual int PointerVal { get; set; }
public virtual int Read(int ofs) { return Marshal.ReadInt32(this.Pointer, ofs); }
public virtual void Write(int ofs, int val) { Marshal.WriteInt32(this.Pointer, ofs, val); }
}
public static class Tools
{
//静态方法
public static void Show(IntPtr ptr, int ofs, int val, string tips)
{
string sVal = string.Format("{0:x8}", (uint)val);
int tmp;
tmp = (val & 0x000000FF) >> 0;
char c1 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = (val & 0x0000FF00) >> 8;
char c2 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = (val & 0x00FF0000) >> 16;
char c3 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = val >> 24;
char c4 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("0x{0:X8} ", (uint)(ptr.ToInt32() + ofs));
Console.ForegroundColor = ConsoleColor.Black;
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]);
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("{0}{1}{2}{3} ", c1, c2, c3, c4);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("{0,-30}", tips);
Console.ResetColor();
}
public static void Show(IntPtr ptr, int val, string tips)
{
Show(ptr, 0, val, tips);
}
public static void ShowArea(IntPtr ptr, int ofs, int len, string tips)
{
int curOfs = ofs;
int tmpVal;
string curTips;
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("{0,-61}","-------------- " + tips + " --------------");
Console.ResetColor();
for (int i = 0; i < len; i++)
{
curTips = string.Format("ofs:{0}", curOfs.ToString());
tmpVal = Marshal.ReadInt32(ptr, curOfs);
Show(ptr, curOfs, tmpVal, curTips);
curOfs += 4;
}
}
public static void ShowArea(IntPtr ptr, int len, string tips)
{
ShowArea(ptr, 0, len, tips);
}
//扩展方法
public static void Show(this IPointable ptr, int ofs, string tips)
{
int val = Marshal.ReadInt32(ptr.Pointer, ofs);
Show(ptr.Pointer, ofs, val, tips);
}
public static void ShowArea(this IPointable ptr, int ofs, int len, string tips)
{
ShowArea(ptr.Pointer, ofs, len, tips);
}
}
//------------ ObjectInstance --------------
// -04(04):SyncblkIndex
// +00(04):MethodTable
// +??(??):ComponentCount
//------------------------------------------
public class ObjectInstance : PointerBase, IPointable, IDisposable
{
private const int SyncblkIndex_Ofs = -4;
private const int TypeHandle_Ofs = 0;
//字段
private object _obj;
private GCHandle _gc;
private IntPtr _ptr_gc;
private IntPtr _ptr;
//属性
public override IntPtr Pointer
{
get { return _ptr; }
set { throw new Exception("non set"); }
}
public override int PointerVal
{
get { return Marshal.ReadInt32(Pointer); }
set { throw new Exception("non set"); }
}
public IntPtr GcPtr { get { return _ptr_gc; } }
public int GcVal { get { return Marshal.ReadInt32(GcPtr); } }
public IntPtr SyncblkIndexPtr { get { return new IntPtr(_ptr.ToInt32() + SyncblkIndex_Ofs); } }
public int SyncblkIndexVal { get { return Marshal.ReadInt32(SyncblkIndexPtr); } }
public IntPtr TypeHandlePtr { get { return new IntPtr(_ptr.ToInt32() + TypeHandle_Ofs); } }
public int TypeHandleVal { get { return Marshal.ReadInt32(TypeHandlePtr); } }
//构造
public ObjectInstance(object obj)
{
if (obj == null) throw new ArgumentNullException();
this._obj = obj;
this._gc = GCHandle.Alloc(obj);
this._ptr_gc = GCHandle.ToIntPtr(_gc);
this._ptr = Marshal.ReadIntPtr(_ptr_gc);
}
//诉构
~ObjectInstance() { (this as IDisposable).Dispose(); }
void IDisposable.Dispose() { if (_gc.IsAllocated)_gc.Free(); }
}
//------------ MethodTable --------------
// -12(12):GCInfo
// +00(04):Flags
// +04(04):Basic Instance Size
// +08(04):EEClass
// +12(04):Interface Vtable Map
// +16(02):NumInterfaces
// +18(02):CorElementType
// +20(04):Module
// +24(04):.cctor Slot
// +26(02):Default .ctor Slot
// +28(04):Interface Map
// +32(04):Delegate
// +36(04):Num Method Slots
// +40(04):ToString...
// +??(04):Equals...
// .......:...........
// +??(04):...........
// +??(04):static string str
// .......:...........
// +??(04):Flags|Impl Start Slot...
// +??(04):MyInterface1 TypeHandle...
// +?2(04):Flags|Impl Start Slot...
// +??(04):MyInterface2 TypeHandle...
// +??(04):...........
// +??(04):...........
//------------------------------------------
public class MethodTable : PointerBase, IPointable
{
}
}
namespace Run
{
using System;
using PCTools.Memory;
interface IA
{
void Test();
}
class A : IA
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public A() { this.i = 100; }
public A(int i) { this.i = i; }
public void Test()
{
Console.WriteLine(I);
}
class MyTest
{
static void Main()
{
A a = new A();
ObjectInstance objectInstance = new ObjectInstance(a);
objectInstance.Show(0, "TypeHandle");
objectInstance.Show(-4, "SyncblkIndex");
Tools.Show(new IntPtr(0), a.GetHashCode(), "a's hashcode");
objectInstance.Show(-4, "SyncblkIndex");
Tools.ShowArea((IntPtr)objectInstance.TypeHandleVal, 20, "TypeHandle'0~20");
Console.ReadKey();
}
}
}
}
复制代码
[attach]53177[/attach]
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3