Beispiel #1
0
        public static void CopyShallow(object source, object target)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            Type t = source.GetType();

            if (target.GetType() != t)
            {
                throw new ArgumentException("Object types must be equal.");
            }

            byte[] data = new byte[UnsafeTools.BaseInstanceSizeOf(t) - IntPtr.Size];
            InteropTools.Pin(
                source, o1 => {
                var ptr = UnsafeTools.GetAddress(o1);
                Marshal.Copy(ptr, data, 0, data.Length);
            }
                );

            InteropTools.Pin(
                target, o2 => {
                var ptr = UnsafeTools.GetAddress(o2);
                Marshal.Copy(data, 0, ptr, data.Length);
            }
                );
        }
Beispiel #2
0
 static StackAllocated()
 {
     if (InteropTools.ContainsReferences <T>())
     {
         throw new InvalidOperationException("Allocating types that contain references on the stack is not supported.");
     }
     if (type.IsArray || type == TypeOf <string> .TypeID)
     {
         throw new InvalidOperationException("Variable-sized instances aren't supported.");
     }
 }
Beispiel #3
0
        public static IntPtr Rva2Offset(uint dwRva, IntPtr PEPointer)
        {
            bool   is64Bit           = false;
            ushort wIndex            = 0;
            ushort wNumberOfSections = 0;
            IntPtr imageSectionPtr;
            IMAGE_SECTION_HEADER SectionHeader;
            int sizeOfSectionHeader = Marshal.SizeOf(typeof(IMAGE_SECTION_HEADER));

            IMAGE_DOS_HEADER dosHeader = InteropTools.PtrToStructure <IMAGE_DOS_HEADER>(PEPointer);

            IntPtr NtHeadersPtr = (IntPtr)((UInt64)PEPointer + (UInt64)dosHeader.e_lfanew);

            var imageNtHeaders32 = (IMAGE_NT_HEADERS)Marshal.PtrToStructure(NtHeadersPtr, typeof(IMAGE_NT_HEADERS));
            var imageNtHeaders64 = (IMAGE_NT_HEADERS64)Marshal.PtrToStructure(NtHeadersPtr, typeof(IMAGE_NT_HEADERS64));

            if (imageNtHeaders64.OptionalHeader.Magic == MagicType.IMAGE_NT_OPTIONAL_HDR64_MAGIC)
            {
                is64Bit = true;
            }


            if (is64Bit)
            {
                imageSectionPtr   = (IntPtr)(((Int64)NtHeadersPtr + (Int64)Marshal.OffsetOf(typeof(IMAGE_NT_HEADERS64), "OptionalHeader") + (Int64)imageNtHeaders64.FileHeader.SizeOfOptionalHeader));
                SectionHeader     = (IMAGE_SECTION_HEADER)Marshal.PtrToStructure(imageSectionPtr, typeof(IMAGE_SECTION_HEADER));
                wNumberOfSections = imageNtHeaders64.FileHeader.NumberOfSections;
            }
            else
            {
                imageSectionPtr   = (IntPtr)(((Int64)NtHeadersPtr + (Int64)Marshal.OffsetOf(typeof(IMAGE_NT_HEADERS), "OptionalHeader") + (Int64)imageNtHeaders32.FileHeader.SizeOfOptionalHeader));
                SectionHeader     = (IMAGE_SECTION_HEADER)Marshal.PtrToStructure(imageSectionPtr, typeof(IMAGE_SECTION_HEADER));
                wNumberOfSections = imageNtHeaders32.FileHeader.NumberOfSections;
            }

            if (dwRva < SectionHeader.PointerToRawData)
            {
                return((IntPtr)((UInt64)dwRva + (UInt64)PEPointer));
            }

            for (wIndex = 0; wIndex < wNumberOfSections; wIndex++)
            {
                SectionHeader = (IMAGE_SECTION_HEADER)Marshal.PtrToStructure((IntPtr)((uint)imageSectionPtr + (uint)(sizeOfSectionHeader * (wIndex))), typeof(IMAGE_SECTION_HEADER));
                if (dwRva >= SectionHeader.VirtualAddress && dwRva < (SectionHeader.VirtualAddress + SectionHeader.SizeOfRawData))
                {
                    return((IntPtr)((UInt64)(dwRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData) + (UInt64)PEPointer));
                }
            }

            return(IntPtr.Zero);
        }
 public static unsafe int GetOffset(this FieldInfo field)
 {
     return(InteropTools.Pin(
                FormatterServices.GetUninitializedObject(field.DeclaringType),
                delegate(object inst)
     {
         TypedReference tr0, tr1;
         TypedReferenceTools.MakeTypedReference(&tr0, inst);
         TypedReferenceTools.MakeTypedReference(&tr1, inst, field);
         byte *p0 = (byte *)tr0.ToPointer();
         byte *p1 = (byte *)tr1.ToPointer();
         return (int)(p1 - p0);
     }
                ));
 }
Beispiel #5
0
        public static void Write <T>(this Stream output, IEnumerable <T> structures) where T : struct
        {
            Type t    = typeof(T);
            int  size = Marshal.SizeOf(t);

            byte[] buffer = new byte[size];
            IntPtr ptr    = Marshal.AllocHGlobal(size);

            try{
                foreach (var s in structures)
                {
                    InteropTools.StructureToPtr(s, ptr);
                    Marshal.Copy(ptr, buffer, 0, size);
                    output.Write(buffer, 0, size);
                }
            }finally{
                Marshal.FreeHGlobal(ptr);
            }
        }
Beispiel #6
0
        private static IEnumerable <T> EnumerateStructures <T>(Stream input) where T : struct
        {
            Type t    = TypeOf <T> .TypeID;
            int  size = Marshal.SizeOf(t);

            byte[] buffer = new byte[size];
            IntPtr ptr    = Marshal.AllocHGlobal(size);

            try{
                while (input.Read(buffer, 0, size) == size)
                {
                    Marshal.Copy(buffer, 0, ptr, size);
                    yield return(InteropTools.PtrToStructure <T>(ptr));
                }
                throw new EndOfStreamException();
            }finally{
                Marshal.FreeHGlobal(ptr);
            }
        }
Beispiel #7
0
 /// <summary>
 /// Pins an object in a memory and constructs its pin handle.
 /// </summary>
 /// <param name="obj">The object to pin.</param>
 public ObjectPinHandle(object obj)
 {
     Object = obj;
     using (var re1 = new AutoResetEvent(false))
     {
         var thr = new Thread(
             delegate()
         {
             InteropTools.Pin(
                 obj,
                 delegate {
                 re1.Set();
                 Reset.WaitOne();
             }
                 );
         }
             );
         thr.Start();
         re1.WaitOne();
     }
 }
Beispiel #8
0
        public static void Write(this Stream output, __arglist)
        {
            ArgIterator iter = new ArgIterator(__arglist);

            try{
                while (iter.GetRemainingCount() > 0)
                {
                    TypedReference tr     = iter.GetNextArg();
                    Type           t      = __reftype(tr);
                    int            size   = Marshal.SizeOf(t);
                    byte[]         buffer = new byte[size];
                    IntPtr         ptr    = Marshal.AllocHGlobal(size);
                    try{
                        InteropTools.StructureToPtrDirect(tr, ptr, size);
                        Marshal.Copy(ptr, buffer, 0, size);
                        output.Write(buffer, 0, size);
                    }finally{
                        Marshal.FreeHGlobal(ptr);
                    }
                }
            }finally{
                iter.End();
            }
        }