Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The Ultimate Toolbox Resource File

0.00/5 (No votes)
25 Aug 2007 1  
A class for enumerating and parsing file based resources from the Ultimate Toolbox

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Source code and project files for this sample can be found in the samples\utility\resfile directory of the sample projects download.

Overview

The COXResourceFile provides a resource with a CSharedFile (thus CFile) interface. One can read or write any type (e.g. a GIF-picture, a movie etc. as in RT_RCDATA) of resources directly through the familiar methods Read(), Write(), Seek(), Flush(), Close(), etc.. The resource will be loaded into memory and wrapped by a COXResourceFile. All changes will be applied to the resource in memory and will be committed when the file is flushed.

The COXResourceLibrary wraps the Win32 API's resource functions (together with COXResourceFile). One object of the COXResourceLibrary corresponds to a library file (an executable: .DLL or .EXE file), whereas one object of the COXResourceFile corresponds to one resource in a library.

Features

  • Direct access of a resource's binary data through the familiar CFile interface.
  • Easy multiple resources update.
  • Minimum code to be written when implementing drag/drop/copy/paste of resources.
  • Easy to enumerate resources in a library file.
  • Flexible parameter data types when specifying the type or name of a resource.

Usage

The samples\utility\resfile sample contains a document class with a COXResourceLibrary m_ResLib member (include OXResourceLibrary.h) and public arrays that will contain the types, names, and languages enumerated:

class CResLibDoc : public CDocument
{
protected: // create from serialization only


    CResLibDoc();
    DECLARE_DYNCREATE(CResLibDoc)

// Attributes


public:
    COXResourceLibrary m_ResLib;
    CStringArray m_sTypes;
    CStringArray m_sNames;
    CWordArray   m_nLangs;

In the OnOpenDocument handler, the selected file is opened and its resources are enumerated:

BOOL CResLibDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
    if (!CDocument::OnOpenDocument(lpszPathName))
        return FALSE;
    
    if (!m_ResLib.Open(lpszPathName))
        {
        AfxMessageBox(_T("Cannot open the specified file."));
        return FALSE;
        }

    if (!m_ResLib.EnumResources(&m_sTypes, &m_sNames, &m_nLangs))
        {
        AfxMessageBox(_T("Cannot enumerate resources."));
        return FALSE;
        }
    return TRUE;
}

Finally, in the views OnInitialUpdate, the arrays are iterated through in order to populate a tree control:

void CResLibView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();
    CTreeCtrl& rTree = GetTreeCtrl();
    rTree.SetImageList(&m_ilFolder, TVSIL_NORMAL); 

    m_nClipFormat = ((CResFileApp*)AfxGetApp())->m_nClipFormat;
    m_pDoc = GetDocument();
    m_pResLib = &m_pDoc->m_ResLib;

    // calling IsModifiable() once instead of repeatedly checking it


    m_bModifiable = m_pResLib->IsModifiable();

    if (m_pResLib == NULL)
        return;

    m_hRootItem = rTree.InsertItem(m_pResLib->GetFileName() + 
        (m_bModifiable ? _T("") : _T(" <<<read />>>")));
    rTree.SetItemData(m_hRootItem, TVI_DATA_FOLDER);

    CString sType, sName, sPrevType;
    HTREEITEM hTypeItem = NULL;
    int iImage = 0;
    for (int i = 0; i < m_pDoc->m_sTypes.GetSize(); i++)            
    // iterate through the resource types enumerated in the document


    {
        sType = m_pDoc->m_sTypes[i];
        sName = m_pDoc->m_sNames[i];
        if (sType != sPrevType)
        {
            sPrevType = sType;
            GetTypeOutput(sType, iImage);

            hTypeItem = rTree.InsertItem(sType, m_hRootItem);
            rTree.SetItemData(hTypeItem, TVI_DATA_FOLDER);
        }

        GetNameOutput(sName, m_pDoc->m_nLangs[i]);

        ASSERT(hTypeItem);
        HTREEITEM hResItem = rTree.InsertItem(sName, iImage, iImage, 
            hTypeItem);
        rTree.SetItemData(hResItem, (DWORD)i);
    }

    rTree.Expand(m_hRootItem, TVE_EXPAND);
    rTree.SortChildren(m_hRootItem);
}

An extended overview and complete class references for these classes is available in the compiled HTML help documentation.

History

Initial CodeProject release August 2007.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here