1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Separate resource into a separate DLL

Discussion in 'Win32' started by shabbir, Jun 26, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    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:
    // 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:
    //{{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:
    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:

  2. ronhash

    ronhash New Member

    Re: Seperate resource into a seperate DLL

    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.

    :confused:

    Thanks!
     
  3. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    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.
     
  4. dilipvasudevan

    dilipvasudevan New Member

    Re: Seperate resource into a seperate DLL

    What Information does the VS_VERSION_INFO contain?
    Will it effect in my dll gettin registered???
     
  5. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    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.
     
  6. dilipvasudevan

    dilipvasudevan New Member

    Re: Seperate resource into a seperate DLL

    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 :confused:
     
  7. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    You don't have the necessary libs/obj for DllRegisterServer / DllUnRegisterServer
     
  8. dilipvasudevan

    dilipvasudevan New Member

    Re: Seperate resource into a seperate DLL

    ok... n wht cld b done abt it???
     
  9. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    Refer http://msdn2.microsoft.com/en-us/library/ms682162.aspx
     
  10. kunal.tawde

    kunal.tawde New Member

    Re: Seperate resource into a seperate DLL

    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...???
     
  11. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    Dialog appears and exits. Looks like you been doing something wrong. Try running the attachment in MS VC compiler preferably 6. It should copy the necessary dll into the correct folder.
     
  12. kunal.tawde

    kunal.tawde New Member

    Re: Seperate resource into a seperate DLL

    Well I exactly followed the steps given by you..
    Not a single change I'd done or missed out anything you directed to...
     
  13. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    Try running the attachment. I have done it and work as expected.
     
  14. kunal.tawde

    kunal.tawde New Member

    Re: Seperate resource into a seperate DLL

    Well Mr.Shabbir I am sorry for the above comments. Everything worked fine. I made a mistake while calling the DLL function...
     
  15. shabbir

    shabbir Administrator Staff Member

    Re: Seperate resource into a seperate DLL

    Ok.
     
  16. kunal.tawde

    kunal.tawde New Member

    Well Shabbir,
    Hi. Try running this using MFC in a Static Library. It gives for an error.
     
  17. shabbir

    shabbir Administrator Staff Member

    And how do I know what is the error unless you give me access to your brain or PC.
     
  18. parvez.yu

    parvez.yu New Member

    i will try to execute and understand this code
     

Share This Page