Click here to Skip to main content
15,894,180 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 134.4K   2.6K   113  
A .NET implementation of a file resource management, with complete support for VS_VERSIONINFO version resources.
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Vestris.ResourceLib
{
    /// <summary>
    /// Extended menu template.
    /// </summary>
    public class MenuExTemplate : MenuTemplateBase
    {
        private User32.MENUEXTEMPLATE _header = new User32.MENUEXTEMPLATE();
        private MenuExTemplateItemCollection _menuItems = new MenuExTemplateItemCollection();

        /// <summary>
        /// Menu items.
        /// </summary>
        public MenuExTemplateItemCollection MenuItems
        {
            get
            {
                return _menuItems;
            }
            set
            {
                _menuItems = value;
            }
        }

        /// <summary>
        /// Read the menu template.
        /// </summary>
        /// <param name="lpRes">Address in memory.</param>
        internal override IntPtr Read(IntPtr lpRes)
        {
            _header = (User32.MENUEXTEMPLATE) Marshal.PtrToStructure(
                lpRes, typeof(User32.MENUEXTEMPLATE));

            IntPtr lpMenuItem = new IntPtr(lpRes.ToInt32() 
                + _header.wOffset // offset from offset field
                + 4 // offset of the offset field
                );

            return _menuItems.Read(lpMenuItem);
        }

        /// <summary>
        /// Write the menu template.
        /// </summary>
        /// <param name="w">Binary stream.</param>
        internal override void Write(System.IO.BinaryWriter w)
        {
            long head = w.BaseStream.Position;
            // write header
            w.Write(_header.wVersion);
            w.Write(_header.wOffset);
            w.Write(_header.dwHelpId);
            // pad to match the offset value
            ResourceUtil.Pad(w, (UInt16) (_header.wOffset - 4));
            // seek to the beginning of the menu item per offset value
            // this may be behind, ie. the help id structure is part of the first popup menu
            w.BaseStream.Seek(head + _header.wOffset + 4, System.IO.SeekOrigin.Begin);
            // write menu items
            _menuItems.Write(w);
        }

        /// <summary>
        /// String representation of the menu in the MENUEX format.
        /// </summary>
        /// <returns>String representation of the menu.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("MENUEX {0}", _header.dwHelpId));
            sb.Append(_menuItems.ToString());
            return sb.ToString();
        }
    }
}

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