예제 #1
0
        /// <summary>
        /// Takes an unmanaged memory address and reads the unmanaged memory given by its pointer,
        /// with memory address validation for added protection
        /// </summary>
        /// <typeparam name="T">the type of structure to return</typeparam>
        /// <param name="memAddress">the unamanaged memory address to read</param>
        /// <returns>the requested structure T</returns>
        public static T ReadStructureSafe <T>(IntPtr memAddress)
        {
            if (UnmanagedMemoryHelper.IsValidMemoryRange(memAddress, RdMarshal.SizeOf(typeof(T))))
            {
                return((T)RdMarshal.PtrToStructure(memAddress, typeof(T)));
            }

            throw new ArgumentException("Bad data pointer - unable to read structure data.");
        }
예제 #2
0
 /// <summary>
 /// Takes an unmanaged memory address and reads the unmanaged memory given by its pointer
 /// </summary>
 /// <typeparam name="T">the type of structure to return</typeparam>
 /// <param name="memAddress">the unmanaged memory address to read</param>
 /// <returns>the requested structure T</returns>
 /// <remarks>use this over ReadStructureSafe for efficiency when there is no doubt about the validity of the pointed to data</remarks>
 public static T ReadStructureUnsafe <T>(IntPtr memAddress)
 {
     // We catch the most basic mistake of passing a null pointer here as it virtually costs nothing to check,
     // but no other checks are made as to the validity of the pointer.
     if (memAddress == IntPtr.Zero)
     {
         throw new ArgumentException("Unexpected null pointer.");
     }
     return((T)RdMarshal.PtrToStructure(memAddress, typeof(T)));
 }