Click here to Skip to main content
15,891,777 members
Articles / Desktop Programming / Windows Forms

Extracting Icons from EXE/DLL and Icon Manipulation

Rate me:
Please Sign up or sign in to vote.
4.83/5 (37 votes)
17 Jan 2009CPOL2 min read 154.7K   11K   84  
How to extract icons from EXE/DLL, split/merge icons, and get icons associated with files.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TAFactory.IconPack
{
    /// <summary>
    /// Represents a resource name (either integer resource or string resource).
    /// </summary>
    public class ResourceName : IDisposable
    {
        #region Properties
        private int? _id;
        /// <summary>
        /// Gets the resource identifier, returns null if the resource is not an integer resource.
        /// </summary>
        public int? Id
        {
            get { return _id; }
            private set { _id = value; }
        }

        private string _name;
        /// <summary>
        /// Gets the resource name, returns null if the resource is not a string resource.
        /// </summary>
        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }

        private IntPtr _value;
        /// <summary>
        /// Gets a pointer to resource name that can be used in FindResource function.
        /// </summary>
        public IntPtr Value
        {
            get
            {
                if (this.IsIntResource)
                    return new IntPtr(this.Id.Value);

                if (this._value == IntPtr.Zero)
                    this._value = Marshal.StringToHGlobalAuto(this.Name);

                return _value;
            }
            private set { _value = value; }
        }

        /// <summary>
        /// Gets whether the resource is an integer resource.
        /// </summary>
        public bool IsIntResource
        {
            get { return (this.Id != null); }
        }
        #endregion

        #region Constructor/Destructor
        /// <summary>
        /// Initializes a new TAFactory.IconPack.ResourceName object.
        /// </summary>
        /// <param name="lpszName">Specifies the resource name. For more ifnormation, see the Remarks section.</param>
        /// <remarks>
        /// If the high bit of lpszName is not set (=0), lpszName specifies the integer identifier of the givin resource.
        /// Otherwise, it is a pointer to a null terminated string.
        /// If the first character of the string is a pound sign (#), the remaining characters represent a decimal number that specifies the integer identifier of the resource. For example, the string "#258" represents the identifier 258.
        /// #define IS_INTRESOURCE(_r) ((((ULONG_PTR)(_r)) >> 16) == 0).
        /// </remarks>
        public ResourceName(IntPtr lpName)
        {
            if (((uint)lpName >> 16) == 0)  //Integer resource
            {
                this.Id = lpName.ToInt32();
                this.Name = null;
            }
            else
            {
                this.Id = null;
                this.Name = Marshal.PtrToStringAuto(lpName);
            }
        }
        /// <summary>
        /// Destructs the ResourceName object.
        /// </summary>
        ~ResourceName()
        {
            Dispose();
        }
        #endregion

        #region Public Functions
        /// <summary>
        /// Returns a System.String that represents the current TAFactory.IconPack.ResourceName.
        /// </summary>
        /// <returns>Returns a System.String that represents the current TAFactory.IconPack.ResourceName.</returns>
        public override string ToString()
        {
            if (this.IsIntResource)
                return "#" + this.Id.ToString();

            return this.Name;
        }
        /// <summary>
        /// Releases the pointer to the resource name.
        /// </summary>
        public void Free()
        {
            if (this._value != IntPtr.Zero)
            {
                try { Marshal.FreeHGlobal(this._value); }
                catch { }
                this._value = IntPtr.Zero;
            }
        }
        #endregion

        #region IDisposable Members
        /// <summary>
        /// Release the pointer to the resource name.
        /// </summary>
        public void Dispose()
        {
            Free();
        }
        #endregion
    }
}

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
Architect RoboteQ
Egypt Egypt
Abdallah Gomah
Master in Computer Science, 2013, Faculty of Computers & Information, Cairo University (Egypt)

- Working as a developer since I graduated from the faculty.
- Like coding in C++ aloso like to code in assembly.
- Have a great experience in coding with .NET (C#/VB), but I prefer the C# notation.
- Had written a lot of applications Desktop and Web.

I love playing football as much as I love the programming.

Comments and Discussions