Separate resource into a separate DLL

shabbir's Avatar author of Separate resource into a separate DLL
This is an article on Separate resource into a separate DLL in Win32.
Its always a good practice to have the resource into a seperate DLL. The major benefit of having the resource into seperate dll is, it can be easily skinned. Also it has the benefit of seperating the GUI from the code which is better for maintainance.

Here are the simple steps to do the same.

Create a new Win32 Dynamic-Link Library With a simple Win32 Dll project.
Add the following resource file to the project
SepRes.rc
Code: CPP
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
    "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
    "#define _AFX_NO_OLE_RESOURCES\r\n"
    "#define _AFX_NO_TRACKER_RESOURCES\r\n"
    "#define _AFX_NO_PROPERTY_RESOURCES\r\n"
    "\r\n"
    "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
    "#ifdef _WIN32\r\n"
    "LANGUAGE 9, 1\r\n"
    "#pragma code_page(1252)\r\n"
    "#endif //_WIN32\r\n"
    "#include ""res\\SepResApp.rc2""  // non-Microsoft Visual C++ edited resources\r\n"
    "#include ""afxres.rc""     // Standard components\r\n"
    "#endif\r\n"
    "\0"
END

/////////////////////////////////////////////////////////////////////////////
#endif    // APSTUDIO_INVOKED


#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE 9, 1
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_ABOUTBOX DIALOG DISCARDABLE  0, 0, 235, 55
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About SepResApp"
FONT 8, "MS Sans Serif"
BEGIN
    ICON            IDR_MAINFRAME,IDC_STATIC,11,17,20,20
    LTEXT           "SepResApp Version 1.0",IDC_STATIC,40,10,119,8,
                    SS_NOPREFIX
    LTEXT           "Copyright (C) 2006",IDC_STATIC,40,25,119,8
    DEFPUSHBUTTON   "OK",IDOK,178,7, 50,14,WS_GROUP
END

IDD_SEPRESAPP_DIALOG DIALOGEX  0, 0, 320, 200
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "SepResApp"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,260,7,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,260,23,50,14
    LTEXT           "TODO: Place dialog controls here.",IDC_STATIC,50,90,200,8
END

/////////////////////////////////////////////////////////////////////////////
//
// Version
//

VS_VERSION_INFO     VERSIONINFO
  FILEVERSION       1,0,0,1
  PRODUCTVERSION    1,0,0,1
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904B0"
        BEGIN
            VALUE "CompanyName",     "\0"
            VALUE "FileDescription", "SepResApp MFC Application\0"
            VALUE "FileVersion",     "1, 0, 0, 1\0"
            VALUE "InternalName",    "SepResApp\0"
            VALUE "LegalCopyright""Copyright (C) 2006\0"
            VALUE "LegalTrademarks", "\0"
            VALUE "OriginalFilename","SepResApp.EXE\0"
            VALUE "ProductName",     "SepResApp Application\0"
            VALUE "ProductVersion""1, 0, 0, 1\0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1200
    END
END

/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE
BEGIN
    IDD_ABOUTBOX, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 228
        TOPMARGIN, 7
        BOTTOMMARGIN, 48
    END
    IDD_SEPRESAPP_DIALOG, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 313
        TOPMARGIN, 7
        BOTTOMMARGIN,193
    END
END
#endif    // APSTUDIO_INVOKED



/////////////////////////////////////////////////////////////////////////////
//
// String Table
//

STRINGTABLE DISCARDABLE
BEGIN
    IDS_ABOUTBOX            "&About SepResApp..."
END
#endif

#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
#define _AFX_NO_SPLITTER_RESOURCES
#define _AFX_NO_OLE_RESOURCES
#define _AFX_NO_TRACKER_RESOURCES
#define _AFX_NO_PROPERTY_RESOURCES

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE 9, 1
#pragma code_page(1252)
#endif //_WIN32
#include "afxres.rc"    // Standard components
#endif

/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED
 
Add the following resource.h file to the project
Code: CPP
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by SEPRESAPP.RC
//
#define IDR_MAINFRAME               128
#define IDM_ABOUTBOX                    0x0010
#define IDD_ABOUTBOX                    100
#define IDS_ABOUTBOX                    101
#define IDD_SEPRESAPP_DIALOG                102

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE    129
#define _APS_NEXT_CONTROL_VALUE  1000
#define _APS_NEXT_SYMED_VALUE      101
#define _APS_NEXT_COMMAND_VALUE  32771
#endif
#endif
Now compile the code and the dll will be created.

Now add new MFC Dialog based application project to the workspace. Remove the rc and the resource.h file from the dialog based application workspace. You can even safely delete them from the Hard disk after removing them from the workspace. or even can edit it to have the version info for the executable.

Now add the following lines to the InitInstance of the application

Code: CPP
HINSTANCE hResourceDll = (HINSTANCE) AfxLoadLibrary(_T("SepResSampleDll.dll"));
if (!hResourceDll)
{
    AfxMessageBox(_T("Error: Cannot find component \"SepResSampleDll.dll\""));
    return FALSE;
}
AfxSetResourceHandle (hResourceDll);
Now copy the dll from the Output folder of the dll project to the location where executable will be created after compiling.

Thats it Run the application.
Attached Files
File Type: zip SepRes.zip (16.1 KB, 103 views)
ronhash's Avatar, Join Date: Sep 2006
Newbie Member
Hi,

Thanks for that tip.

I don't understand how can i keep my version on the executale if i remove the resources from the executable, and point to another resource file.



Thanks!
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You can have a resource file in the executables having the VS_VERSION_INFO info and move the other resources like the dialogs/bitmaps ... to a seperate dll.
dilipvasudevan's Avatar, Join Date: Mar 2007
Newbie Member
Quote:
Originally Posted by shabbir
You can have a resource file in the executables having the VS_VERSION_INFO info and move the other resources like the dialogs/bitmaps ... to a seperate dll.
What Information does the VS_VERSION_INFO contain?
Will it effect in my dll gettin registered???
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by dilipvasudevan
What Information does the VS_VERSION_INFO contain?
Will it effect in my dll gettin registered???
It contains the version information about your Dll. The info that you see when you right click on any file and click on version tab.
dilipvasudevan's Avatar, Join Date: Mar 2007
Newbie Member
hmmm... but will the version mentioned over there has any effect on my Dll getting registered upon build???
well, wht actually happenin over here is tht i'm getting an error upon build.
Either it says
1. unresolved external symbols DllRegisterServer & DllUnRegisterServer alongwith failure during build of exports file. OR
2. A tool returned an error code from "Performing Registration"

Do u know y i'm gettin these
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You don't have the necessary libs/obj for DllRegisterServer / DllUnRegisterServer
dilipvasudevan's Avatar, Join Date: Mar 2007
Newbie Member
ok... n wht cld b done abt it???
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by dilipvasudevan
ok... n wht cld b done abt it???
Quote:
Originally Posted by MSDN
For an explanation of the requirement values, see Requirements (COM).
Header: Declared in olectl.h.
Refer http://msdn2.microsoft.com/en-us/library/ms682162.aspx
kunal.tawde's Avatar, Join Date: May 2007
Light Poster
Well I followed exactly same procedure as mentioned above, but after copying the dll into the Output folder of the MFC Dialog Application project the dialog doesn't appear and exits with the code 0 (0x0)..
May I know any solution for this...???