Click here to Skip to main content
15,887,135 members
Articles / Desktop Programming / Win32

File Resource Management Library (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (27 votes)
16 Sep 2009CPOL14 min read 134K   2.6K   113  
A .NET implementation of a file resource management, with complete support for VS_VERSIONINFO version resources.
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.IO;

namespace Vestris.ResourceLib
{
    /// <summary>
    /// This structure depicts the organization of data in a group icon resource.
    /// </summary>
    public class GroupIconResource : Resource
    {
        public enum GroupType
        {
            Icon = 1,
            Cursor = 2
        };

        Kernel32.GRPICONDIR _header = new Kernel32.GRPICONDIR();
        List<IconResource> _icons = new List<IconResource>();

        /// <summary>
        /// Type of the group icon resource.
        /// </summary>
        public GroupType GroupIconResourceType
        {
            get
            {
                return (GroupType) _header.wType;
            }
            set
            {
                _header.wType = (byte) value;
            }
        }

        public List<IconResource> Icons
        {
            get
            {
                return _icons;
            }
            set
            {
                _icons = value;
            }
        }

        /// <summary>
        /// An icon resource
        /// </summary>
        public GroupIconResource(IntPtr hModule, IntPtr hResource, IntPtr type, IntPtr name, ushort wIDLanguage, int size)
            : base(hModule, hResource, type, name, wIDLanguage, size)
        {
            IntPtr lpRes = Kernel32.LockResource(hResource);

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

            Read(hModule, lpRes);
        }

        public GroupIconResource()
            : base(IntPtr.Zero, 
                IntPtr.Zero, 
                new IntPtr((uint)Kernel32.ResourceTypes.RT_GROUP_ICON), 
                new IntPtr(1), 
                1033, 
                Marshal.SizeOf(typeof(Kernel32.GRPICONDIR)))
        {
            GroupIconResourceType = GroupType.Icon;
        }

        /// <summary>
        /// Load from an executable file
        /// </summary>
        /// <param name="filename">an executable file (.exe or .dll)</param>
        public void LoadFrom(string filename)
        {
            base.LoadFrom(filename, new IntPtr(1), 
                new IntPtr((uint) Kernel32.ResourceTypes.RT_GROUP_ICON));
        }

        public void SaveTo(string filename)
        {
            base.SaveTo(filename, new IntPtr(int.Parse(Name)), 
                new IntPtr((uint) Kernel32.ResourceTypes.RT_GROUP_ICON), Language);

            foreach (IconResource icon in _icons)
            {
                icon.SaveIconTo(filename);
            }
        }

        public override IntPtr Read(IntPtr hModule, IntPtr lpRes)
        {
            _icons.Clear();

            _header = (Kernel32.GRPICONDIR) Marshal.PtrToStructure(
                lpRes, typeof(Kernel32.GRPICONDIR));

            IntPtr pEntry = new IntPtr(lpRes.ToInt32() + Marshal.SizeOf(_header));

            for (int i = 0; i < _header.wImageCount; i++)
            {
                IconResource iconResource = new IconResource();
                pEntry = iconResource.Read(hModule, pEntry);
                _icons.Add(iconResource);
            }

            return pEntry;
        }

        public override void Write(BinaryWriter w)
        {
            w.Write((UInt16) _header.wReserved);
            w.Write((UInt16) _header.wType);
            w.Write((UInt16) _icons.Count);
            ResourceUtil.PadToWORD(w);
            foreach(IconResource icon in _icons)
            {
                icon.Write(w);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
dB.
Team Leader Application Security Inc., www.appsecinc.com
United States United States
Daniel Doubrovkine has been in software engineering for twelve years and is currently development manager at Application Security Inc. in New York City. He has been involved in many software ventures, including Xo3 and Vestris Inc, was a development lead at Microsoft Corp. in Redmond, and director of Engineering at Visible Path Corp. in New York City. Daniel also builds and runs a foodie website, http://www.foodcandy.com.

Comments and Discussions