public static object ReadValue(this IntPtr baseAddress, ref int offset, ItemType type) { object val = null; switch (type) { case ItemType.Int8: val = (sbyte)Marshal.ReadByte(baseAddress, offset); break; case ItemType.UInt8: val = Marshal.ReadByte(baseAddress, offset); break; case ItemType.Bool: case ItemType.UInt16: val = (ushort)Marshal.ReadInt16(baseAddress, offset); break; case ItemType.Int16: val = Marshal.ReadInt16(baseAddress, offset); break; case ItemType.UInt32: val = (uint)Marshal.ReadInt32(baseAddress, offset); break; case ItemType.Int32: val = Marshal.ReadInt32(baseAddress, offset); break; case ItemType.Fix32: TWFix32 f32 = new TWFix32(); f32.Whole = Marshal.ReadInt16(baseAddress, offset); f32.Fraction = (ushort)Marshal.ReadInt16(baseAddress, offset + 2); val = f32; break; case ItemType.Frame: TWFrame frame = new TWFrame(); frame.Left = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); frame.Top = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); frame.Right = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); frame.Bottom = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); return(frame); // no need to update offset again after reading fix32 case ItemType.String128: case ItemType.String255: case ItemType.String32: case ItemType.String64: val = Marshal.PtrToStringAnsi(new IntPtr(baseAddress.ToInt64() + offset)); break; case ItemType.Handle: val = Marshal.ReadIntPtr(baseAddress, offset); break; } offset += GetItemTypeSize(type); return(val); }
public static void WriteValue(this IntPtr baseAddr, ref int offset, ItemType type, object value) { switch (type) { case ItemType.Int8: case ItemType.UInt8: Marshal.WriteByte(baseAddr, offset, Convert.ToByte(value, CultureInfo.InvariantCulture)); // (byte)value); break; case ItemType.Bool: case ItemType.Int16: case ItemType.UInt16: Marshal.WriteInt16(baseAddr, offset, Convert.ToInt16(value, CultureInfo.InvariantCulture)); //(short)value); break; case ItemType.UInt32: case ItemType.Int32: Marshal.WriteInt32(baseAddr, offset, Convert.ToInt32(value, CultureInfo.InvariantCulture)); //(int)value); break; case ItemType.Fix32: TWFix32 f32 = (TWFix32)value; Marshal.WriteInt16(baseAddr, offset, f32.Whole); if (f32.Fraction > Int16.MaxValue) { Marshal.WriteInt16(baseAddr, offset + 2, (Int16)(f32.Fraction - 32768)); } else { Marshal.WriteInt16(baseAddr, offset + 2, (Int16)f32.Fraction); } break; case ItemType.Frame: TWFrame frame = (TWFrame)value; WriteValue(baseAddr, ref offset, ItemType.Fix32, (TWFix32)frame.Left); WriteValue(baseAddr, ref offset, ItemType.Fix32, (TWFix32)frame.Top); WriteValue(baseAddr, ref offset, ItemType.Fix32, (TWFix32)frame.Right); WriteValue(baseAddr, ref offset, ItemType.Fix32, (TWFix32)frame.Bottom); return; // no need to update offset for this //case ItemType.String1024: // WriteString(baseAddr, offset, value as string, 1024); // break; case ItemType.String128: WriteString(baseAddr, offset, (string)value, 128); break; case ItemType.String255: WriteString(baseAddr, offset, (string)value, 255); break; case ItemType.String32: WriteString(baseAddr, offset, (string)value, 32); break; case ItemType.String64: WriteString(baseAddr, offset, (string)value, 64); break; //case ItemType.Unicode512: // WriteUString(baseAddr, offset, value as string, 512); // break; } offset += TypeExtensions.GetItemTypeSize(type); }