示例#1
0
        public static UnmanagedMemoryAccessor LoadResourceAccessor(
            this SafeModuleHandle module, ResInfoHandle resource)
        {
            var ptr  = module.LoadResource(resource);
            var size = (long)ptr.ByteLength;

            return(new UnmanagedMemoryAccessor(ptr, 0, size, FileAccess.Read));
        }
示例#2
0
        public static unsafe UnmanagedMemoryStream LoadResourceStream(
            this SafeModuleHandle module, ResInfoHandle resInfo)
        {
            var ptr  = module.LoadResource(resInfo);
            var size = (long)ptr.ByteLength;

            return(new UnmanagedMemoryStream(
                       ptr.DangerousGetPointer(), size, size, FileAccess.Read));
        }
示例#3
0
        public static byte[] LoadResourceData(
            this SafeModuleHandle module, ResInfoHandle resource)
        {
            var ptr  = module.LoadResource(resource);
            var size = (long)ptr.ByteLength;
            var data = new byte[size];

            Marshal.Copy(ptr.DangerousGetHandle(), data, 0, data.Length);
            return(data);
        }
示例#4
0
        public static T LoadResourceData <T>(
            this SafeModuleHandle module, ResInfoHandle resource)
        {
            var ptr  = module.LoadResource(resource);
            var size = (long)ptr.ByteLength;
            var type = typeof(T);

            if (size != Marshal.SizeOf(type))
            {
                return(default(T));
            }

            if (type.IsValueType && !type.IsPrimitive)
            {
                return(Marshal.PtrToStructure <T>(ptr.DangerousGetHandle()));
            }

            if (type == typeof(sbyte) || type == typeof(byte))
            {
                return((T)(object)Marshal.ReadByte(ptr.DangerousGetHandle()));
            }
            if (type == typeof(short) || type == typeof(ushort))
            {
                return((T)(object)Marshal.ReadInt16(ptr.DangerousGetHandle()));
            }
            if (type == typeof(int) || type == typeof(uint))
            {
                return((T)(object)Marshal.ReadInt32(ptr.DangerousGetHandle()));
            }
            if (type == typeof(long) || type == typeof(ulong))
            {
                return((T)(object)Marshal.ReadInt64(ptr.DangerousGetHandle()));
            }

            throw new ArgumentException("Unable to marshal to type T.");
        }