/// <summary>
        ///   Loads the specified module.
        /// </summary>
        /// <param name="fileName">
        ///   A string that specifies the file name of the module to load.
        /// </param>
        /// <param name="flags">
        ///   The action to be taken when loading the module.
        /// </param>
        /// <returns>
        ///   A <see cref="SafeModuleHandle"/> to the loaded module.
        /// </returns>
        public static SafeModuleHandle Load(string fileName, LoadLibraryExFlags flags = 0)
        {
            SafeModuleHandle handle = ResourceUnsafeNativeMethods.LoadLibraryEx(
                fileName, IntPtr.Zero, flags);

            if (handle.IsInvalid)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            return(handle);
        }
示例#2
0
        public static List <ResourceName> GetResourceTypesEx(
            SafeModuleHandle module)
        {
            var names = new List <ResourceName>();

            EnumResourceTypesEx(
                module,
                (m, type, p) => {
                names.Add(ResourceName.FromPtr(type));
                return(true);
            },
                IntPtr.Zero,
                0,
                0);
            return(names);
        }
示例#3
0
        public static string LoadString(SafeModuleHandle hInstance, uint uID)
        {
            var buffer = new StringBuilder(64);

            while (true)
            {
                int count = LoadString(hInstance, uID, buffer, buffer.Capacity);
                if (count == 0)
                {
                    return(null);
                }
                if (count < buffer.Capacity - 1)
                {
                    break;
                }
                buffer.Capacity *= 2;
            }

            return(buffer.ToString());
        }
示例#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.");
        }
 public static extern HResult GetThemeStream(
     SafeThemeHandle hTheme, int iPartId, int iStateId, int iPropId,
     out IntPtr ppvStream, out uint pcbStream, SafeModuleHandle hInst);
示例#6
0
 public static ResInfoHandle FindResourceEx(
     this SafeModuleHandle module, ResourceName type, ResourceName name, short language)
 {
     return(ResourceUnsafeNativeMethods.FindResourceEx(module, type, name, language));
 }
示例#7
0
 public static uint ResourceSize(this SafeModuleHandle module, ResInfoHandle resource)
 {
     return(ResourceUnsafeNativeMethods.SizeofResource(module, resource));
 }
示例#8
0
 public static extern bool EnumResourceTypesEx(
     SafeModuleHandle hModule,
     EnumResTypeProc lpEnumFunc,
     IntPtr lParam,
     uint dwFlags,
     uint LangId);
示例#9
0
 public static extern int LoadString(
     SafeModuleHandle hInstance, uint uID, [Out] StringBuilder lpBuffer, int nBufferMax);
示例#10
0
 public static extern uint SizeofResource(
     SafeModuleHandle hModule, ResInfoHandle hResInfo);
示例#11
0
 public static extern ResDataHandle LoadResource(
     SafeModuleHandle hModule, ResInfoHandle hResInfo);
示例#12
0
 public static extern ResInfoHandle FindResource(
     SafeModuleHandle hModule,
     [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ResourceNameMarshaler))]
     ResourceName lpName,
     [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ResourceNameMarshaler))]
     ResourceName lpType);