internal static object MarshalFromUnmanaged(uint tomarshal, UnmanagedType type, uint sizeconst, Type knowntype) { if (type == UnmanagedType.Interface) { if (knowntype == typeof(UnmanagedBuffer)) { return((object)new UnmanagedBuffer((IntPtr)tomarshal)); } else { return(UnmanagedObject.Create(knowntype, (IntPtr)tomarshal)); } } else if (type == UnmanagedType.LPStruct) { UnmanagedBuffer buff = new UnmanagedBuffer((IntPtr)tomarshal); return(buff.ReadAt(0, knowntype)); } else if (type == UnmanagedType.LPWStr) { UnmanagedBuffer buff = new UnmanagedBuffer((IntPtr)tomarshal); return((object)buff.ReadUnicodeStringAt(0)); } else if (type == UnmanagedType.LPStr) { UnmanagedBuffer buff = new UnmanagedBuffer((IntPtr)tomarshal); return((object)buff.ReadStringAt(0)); } else { return((object)tomarshal); } }
public uint this[uint index] { get { return(m_stack.ReadAt <uint>((long)index * 4)); } set { m_stack.WriteAt <uint>(value, (long)index * 4); } }
//currently unused //looks up the packet size in the client's internal packetinfo table... //if it's fixed size true is returned and size is the size; //for dynamic size packets false is returned private bool GetPacketSize(byte packetnumber, out ushort size) { size = m_PacketInfo.ReadAt <ushort>(packetnumber * 3 * 4); if ((size == 0x8000) || (size == 0)) { return(false); } else { return(true); } }
private static bool ActualHook(uint pAddress) { LocalHook curhook; UnmanagedBuffer buff; UnmanagedContext ctx; UnmanagedStack stck; uint esp; if (m_Hooks.ContainsKey(pAddress)) { curhook = m_Hooks[pAddress]; esp = curhook.m_EspBackup.ReadAt <uint>(0); buff = new UnmanagedBuffer((IntPtr)esp); ctx = buff.ReadAt <UnmanagedContext>(0); stck = new UnmanagedStack(esp + 40); curhook.m_ReturnAddresses.Push(buff.ReadAt <uint>(36)); buff.WriteAt <uint>(curhook.m_LateHookAddress, 36); return(curhook.InvokeOnCall(ctx, stck)); } return(true); }
public UnmanagedProxy(Type unmanagedinterface, IntPtr address, UnmanagedObject tokeepalive) { m_ToKeepAlive = tokeepalive; m_Interface = unmanagedinterface; m_Address = address; EventInfo[] events = unmanagedinterface.GetEvents(); if (events.Length > 0) { m_EventsByAddHandler = new Dictionary <int, UnmanagedEvent>(events.Length); m_EventsByRemoveHandler = new Dictionary <int, UnmanagedEvent>(events.Length); } foreach (EventInfo ev in events) { UnmanagedEvent uev; object[] attribs = ev.GetCustomAttributes(typeof(UHookAttribute), true); if (attribs.Length > 0) { UHookAttribute uha = (UHookAttribute)attribs[0]; if (uha.IsVtblHook) { UnmanagedBuffer tempbuffer = new UnmanagedBuffer((IntPtr)address); uint m_addr = tempbuffer.ReadAt <uint>(0); uev = new UnmanagedEvent(ev, (uint)m_addr + 4 * uha.Address, uha.StackSize, uha.IsVtblHook, uha.IsEarly); } else { uint m_addr = (uint)UOCallibration.Callibrations[uha.Address]; uev = new UnmanagedEvent(ev, m_addr, uha.StackSize, uha.IsVtblHook, uha.IsEarly); } m_EventsByAddHandler.Add((int)ev.GetAddMethod().MethodHandle.Value, uev); m_EventsByRemoveHandler.Add((int)ev.GetRemoveMethod().MethodHandle.Value, uev); } else if ((attribs = ev.GetCustomAttributes(typeof(UForwardAttribute), true)).Length > 0) { uev = new UnmanagedEvent((UForwardAttribute)attribs[0]); m_EventsByAddHandler.Add((int)ev.GetAddMethod().MethodHandle.Value, uev); m_EventsByRemoveHandler.Add((int)ev.GetRemoveMethod().MethodHandle.Value, uev); } } }
//perform unmanaged call public static uint Call(uint tocall, uint[] stack, uint thispar) { uint toreturn = 0; UnmanagedBuffer stackbuffer = new UnmanagedBuffer((uint)stack.Length * 4); for (uint i = 0; i < stack.Length; i++)//write reversed stack array { stackbuffer.WriteAt <uint>(stack[stack.Length - 1 - i], i * 4); } m_Parameters.WriteAt <uint>(tocall, data_alignment + 0 * 4); m_Parameters.WriteAt <uint>((uint)stack.Length, data_alignment + 1 * 4); m_Parameters.WriteAt <uint>((uint)stackbuffer.Address, data_alignment + 2 * 4); m_Parameters.WriteAt <uint>(thispar, data_alignment + 3 * 4); m_Call.DynamicInvoke(new object[] { }); toreturn = m_Parameters.ReadAt <uint>(data_alignment + 4 * 4); stackbuffer.Free(); return(toreturn); }
public IMessage Invoke(IMessage message) { IMessage returnMessage; object[] attribs; uint addr; uint retval; IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)message); MethodBase method = methodMessage.MethodBase; object returnValue = null; if (method.MethodHandle == GetTypeMethodHandle) { returnValue = m_Interface; } else if (((attribs = method.GetCustomAttributes(typeof(SyncAttribute), true)).Length > 0) && (Client.Default.InvokeRequired)) { //m_Async value overrides Sync.Async if (m_Async.ContainsKey((int)method.MethodHandle.Value)) { if ((m_Async[(int)method.MethodHandle.Value]))//async override { Client.Default.BeginInvoke(new InvokeDelegate(Invoke), new object[] { message }); return(new ReturnMessage(null, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage)); } else//sync override { return((IMessage)Client.Default.Invoke(new InvokeDelegate(Invoke), new object[] { message })); } } else if (((SyncAttribute)attribs[0]).Async)//preconfigured async { Client.Default.BeginInvoke(new InvokeDelegate(Invoke), new object[] { message }); return(new ReturnMessage(null, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage)); } else//preconfigured sync { return((IMessage)Client.Default.Invoke(new InvokeDelegate(Invoke), new object[] { message })); } } else { if ((attribs = method.GetCustomAttributes(typeof(UMethodAttribute), true)).Length > 0) { UMethodAttribute mattrib = (UMethodAttribute)attribs[0]; addr = (uint)UOCallibration.Callibrations[(uint)mattrib.CallibratedOffsetFeature]; retval = PerformCall(addr, methodMessage); if (mattrib.HasReturnValue) { returnValue = MarshalFromUnmanaged(retval, mattrib.ReturnType, mattrib.SizeConstant, mattrib.KnownType); } else { returnValue = null; } } else if ((attribs = method.GetCustomAttributes(typeof(UFieldAttribute), true)).Length > 0) { UFieldAttribute fattrib = (UFieldAttribute)attribs[0]; UnmanagedBuffer buff = new UnmanagedBuffer((IntPtr)((uint)m_Address + (uint)UOCallibration.Callibrations[(uint)fattrib.CallibratedOffsetFeature])); retval = buff.Read <uint>(); returnValue = MarshalFromUnmanaged(retval, fattrib.ReturnType, fattrib.SizeConstant, fattrib.KnownType); } else if ((attribs = method.GetCustomAttributes(typeof(UGlobalAttribute), true)).Length > 0) { UGlobalAttribute gattrib = (UGlobalAttribute)attribs[0]; UnmanagedBuffer buff = new UnmanagedBuffer((IntPtr)UOCallibration.Callibrations[(uint)gattrib.CallibratedOffsetFeature]); retval = buff.Read <uint>(); returnValue = MarshalFromUnmanaged(retval, gattrib.ReturnType, gattrib.SizeConstant, gattrib.KnownType); } else if ((attribs = method.GetCustomAttributes(typeof(UVtblMethodAttribute), true)).Length > 0) { UVtblMethodAttribute mattrib = (UVtblMethodAttribute)attribs[0]; UnmanagedBuffer m_This = new UnmanagedBuffer(m_Address); addr = m_This.ReadAt <uint>(0); if (mattrib.hasidx) { addr += 4 * mattrib.index; } else { addr += (uint)UOCallibration.Callibrations[(uint)mattrib.CallibratedOffsetFeature]; } retval = PerformCall(addr, methodMessage); if (mattrib.HasReturnValue) { returnValue = MarshalFromUnmanaged(retval, mattrib.ReturnType, mattrib.SizeConstant, mattrib.KnownType); } else { returnValue = null; } } else if ((attribs = method.GetCustomAttributes(typeof(UForwardAttribute), true)).Length > 0) { UForwardAttribute fattrib = (UForwardAttribute)attribs[0]; /*if (fattrib.Target == null) * { * if (m_EventsByAddHandler.ContainsKey((int)method.MethodHandle.Value)) * returnValue = fattrib.onAdd.Invoke(null, methodMessage.Args); * else if (m_EventsByRemoveHandler.ContainsKey((int)method.MethodHandle.Value)) * returnValue = fattrib.onRemove.Invoke(null, methodMessage.Args); * else * returnValue = null; * } * else*/ returnValue = fattrib.Target.Invoke(null, methodMessage.Args); } else if ((methodMessage.ArgCount == 1) && (methodMessage.Args[0] is Delegate)) { returnValue = null; if (m_EventsByAddHandler.ContainsKey((int)method.MethodHandle.Value)) { m_EventsByAddHandler[(int)method.MethodHandle.Value].Add((Delegate)methodMessage.Args[0]); } else if (m_EventsByRemoveHandler.ContainsKey((int)method.MethodHandle.Value)) { m_EventsByRemoveHandler[(int)method.MethodHandle.Value].Remove((Delegate)methodMessage.Args[0]); } } } returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage); return(returnMessage); }