Exemplo n.º 1
0
        public static ResourceBuffer LoadResource(
            this SafeModuleHandle module, ResInfoHandle resource)
        {
            if (resource.IsInvalid)
            {
                throw new ArgumentException("Invalid resource info handle.");
            }

            ResDataHandle data = ResourceUnsafeNativeMethods.LoadResource(module, resource);

            if (data.IsInvalid)
            {
                throw new Win32Exception();
            }

            uint size = ResourceUnsafeNativeMethods.SizeofResource(module, resource);

            if (size == 0)
            {
                throw new Win32Exception();
            }

            ResourceBuffer ptr = ResourceUnsafeNativeMethods.LockResource(data);

            ptr.Initialize(size);

            return(ptr);
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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.");
        }
Exemplo n.º 6
0
 public static uint ResourceSize(this SafeModuleHandle module, ResInfoHandle resource)
 {
     return(ResourceUnsafeNativeMethods.SizeofResource(module, resource));
 }
Exemplo n.º 7
0
 public static extern uint SizeofResource(
     SafeModuleHandle hModule, ResInfoHandle hResInfo);
Exemplo n.º 8
0
 public static extern ResDataHandle LoadResource(
     SafeModuleHandle hModule, ResInfoHandle hResInfo);