/// <summary>
        ///     A structured resource embedded in an executable module.
        /// </summary>
        /// <param name="hModule">Module handle.</param>
        /// <param name="hResource">Resource handle.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="language">Language ID.</param>
        /// <param name="size">Resource size.</param>
        internal Resource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size) {
            _hModule = hModule;
            _type = type;
            _name = name;
            _language = language;
            _hResource = hResource;
            _size = size;

            LockAndReadResource(hModule, hResource);
        }
 /// <summary>
 ///     Create a new icon image resource from a file icon.
 /// </summary>
 /// <param name="icon">File icon.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource id.</param>
 /// <param name="language">Resource language.</param>
 public IconImageResource(IconFileIcon icon, ResourceId type, ResourceId name, UInt16 language) {
     _name = name;
     _type = type;
     _language = language;
     _header.bColors = icon.Header.bColors;
     _header.bHeight = icon.Header.bHeight;
     _header.bReserved = icon.Header.bReserved;
     _header.bWidth = icon.Header.bWidth;
     _header.dwImageSize = icon.Header.dwImageSize;
     _header.wBitsPerPixel = icon.Header.wBitsPerPixel;
     _header.wPlanes = icon.Header.wPlanes;
     _header.nID = (UInt16)name.Id;
     _image = new DeviceIndependentBitmap(icon.Image);
 }
 /// <summary>
 ///     An existing embedded manifest resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 public ManifestResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size) {
 }
 /// <summary>
 ///     An existing font resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 public FontDirectoryResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size) {
 }
        /// <summary>
        ///     Save a resource to an executable (.exe or .dll) file.
        /// </summary>
        /// <param name="filename">Path to an executable file.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="lang">Resource language.</param>
        /// <param name="data">Resource data.</param>
        internal static void SaveTo(string filename, ResourceId type, ResourceId name, UInt16 lang, byte[] data) {
            var h = Kernel32.BeginUpdateResource(filename, false);

            if (h == IntPtr.Zero) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!Kernel32.UpdateResource(h, type.Id, name.Id, lang, data, (data == null ? 0 : (uint)data.Length))) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!Kernel32.EndUpdateResource(h, false)) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
 /// <summary>
 ///     Delete a resource from an executable (.exe or .dll) file.
 /// </summary>
 /// <param name="filename">Path to an executable file.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="lang">Resource language.</param>
 public static void Delete(string filename, ResourceId type, ResourceId name, UInt16 lang) {
     SaveTo(filename, type, name, lang, null);
 }
 /// <summary>
 ///     Save a resource to an executable (.exe or .dll) file.
 /// </summary>
 /// <param name="filename">Path to an executable file.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="langid">Language id.</param>
 internal void SaveTo(string filename, ResourceId type, ResourceId name, UInt16 langid) {
     var data = WriteAndGetBytes();
     SaveTo(filename, type, name, langid, data);
 }
        /// <summary>
        ///     Load a resource from an executable (.exe or .dll) module.
        /// </summary>
        /// <param name="hModule">An executable (.exe or .dll) module.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="lang">Resource language.</param>
        internal void LoadFrom(IntPtr hModule, ResourceId type, ResourceId name, UInt16 lang) {
            if (IntPtr.Zero == hModule) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var hRes = Kernel32.FindResourceEx(hModule, type.Id, name.Id, lang);
            if (IntPtr.Zero == hRes) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var hGlobal = Kernel32.LoadResource(hModule, hRes);
            if (IntPtr.Zero == hGlobal) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var lpRes = Kernel32.LockResource(hGlobal);

            if (lpRes == IntPtr.Zero) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            _size = Kernel32.SizeofResource(hModule, hRes);
            if (_size <= 0) {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            _type = type;
            _name = name;
            _language = lang;

            Read(hModule, lpRes);
        }
        /// <summary>
        ///     Load a resource from an executable (.exe or .dll) file.
        /// </summary>
        /// <param name="filename">An executable (.exe or .dll) file.</param>
        /// <param name="name">Resource name.</param>
        /// <param name="type">Resource type.</param>
        /// <param name="lang">Resource language.</param>
        internal void LoadFrom(string filename, ResourceId type, ResourceId name, UInt16 lang) {
            var hModule = IntPtr.Zero;

            try {
                hModule = Kernel32.LoadLibraryEx(filename, IntPtr.Zero, Kernel32Contants.DONT_RESOLVE_DLL_REFERENCES | Kernel32Contants.LOAD_LIBRARY_AS_DATAFILE);

                LoadFrom(hModule, type, name, lang);
            } finally {
                if (hModule != IntPtr.Zero) {
                    Kernel32.FreeLibrary(hModule);
                }
            }
        }
 /// <summary>
 ///     Convert into an icon resource that can be written into an executable.
 /// </summary>
 /// <param name="icon">Icon image.</param>
 /// <param name="id">Icon Id.</param>
 /// <param name="language">Resource language.</param>
 /// <returns>An icon resource.</returns>
 public IconResource(IconFileIcon icon, ResourceId id, UInt16 language) : base(icon, new ResourceId(ResourceTypes.RT_ICON), id, language) {
 }
 /// <summary>
 ///     An existing icon resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 internal IconResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size) {
 }
 /// <summary>
 ///     A generic resource.
 /// </summary>
 /// <param name="name">Resource name.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="language">Resource language.</param>
 public GenericResource(ResourceId type, ResourceId name, UInt16 language) {
     _type = type;
     _name = name;
     _language = language;
 }
 /// <summary>
 ///     Convert into an icon resource that can be written into an executable.
 /// </summary>
 /// <param name="icon">Icon image.</param>
 /// <param name="id">Icon Id.</param>
 /// <param name="language">Language.</param>
 /// <returns>An icon resource.</returns>
 public CursorResource(IconFileIcon icon, ResourceId id, UInt16 language) : base(icon, new ResourceId(ResourceTypes.RT_CURSOR), id, language) {
 }
 /// <summary>
 ///     A new icon resource.
 /// </summary>
 public IconImageResource(ResourceId type)
     : base(IntPtr.Zero, IntPtr.Zero, type, new ResourceId(IntPtr.Zero), ResourceUtil.NEUTRALLANGID, Marshal.SizeOf(typeof (GrpIconDirEntry))) {
 }
 /// <summary>
 ///     An existing accelerator resource.
 /// </summary>
 /// <param name="hModule">Module handle.</param>
 /// <param name="hResource">Resource ID.</param>
 /// <param name="type">Resource type.</param>
 /// <param name="name">Resource name.</param>
 /// <param name="language">Language ID.</param>
 /// <param name="size">Resource size.</param>
 public AcceleratorResource(IntPtr hModule, IntPtr hResource, ResourceId type, ResourceId name, UInt16 language, int size)
     : base(hModule, hResource, type, name, language, size) {
 }
 /// <summary>
 ///     A new string resource of a given block id.
 /// </summary>
 /// <param name="blockId">Block id.</param>
 public StringResource(ResourceId blockId)
     : base(IntPtr.Zero, IntPtr.Zero, new ResourceId(ResourceTypes.RT_STRING), blockId, ResourceUtil.NEUTRALLANGID, 0) {
 }