Creating a view class as a resource

Posted on 2004-12-01
Last Modified: 2013-11-20

Is it possible to have a view class as a resource in an MFC application? (i.e. defined in .rc and with a resource ID).If yes, how can it be done?

Question by:sathishkumar
    LVL 48

    Expert Comment

    Take a look on CFormView class, it is based on dialog resource.

    Author Comment

    We have a view class(say ViewDll) in a dll inheriting from CView. In our application(exe) we have a different view class (say ViewApp) which inherits from CFormView.

    As per the requirement, we are trying to have the ViewDll class inheriting from ViewApp.
    When we tried to instantiate the ViewDll class, we get a compilation error saying "There is no default constructor available for ViewApp class".

    The ViewApp class constructor accepts a resource id corresponding to the type of view needed. we are trying to achive this by

    ViewDll :: ViewDll() : ViewApp(int ResourceID).

    In this case also we get the same compilation error.


    Author Comment

    The compilation error is sorted out. But there is an link error saying that "cannot open file ViewDll.lib"...  The ViewDll.lib is supposed to be created after the ViewDll is built.

    The exact message in my workspace is as follows:

    LINK : fatal error LNK1104: cannot open file "../lib/ViewDll.lib"
    LINK : fatal error LNK1141: failure during build of exports file
    Error executing link.exe.

    LVL 49

    Expert Comment

    A view is really a block of program code, while a resource is (almost always) a block of data.  I don't think there is any *normal* way to include a View as a program resource.

    If the resource ID refers to a dialog resource, then that is possible, but your question is unclear as to that regard.

    Please explain your need very specifically. Describe what you want the end result to be.

    Author Comment

    The end result is that I want to succeed in making the inheritance of ViewDll class from ViewApp class which are detailed below.

    1) ViewDll class is in an dll application.
    2)ViewDll is inheriting from CView.

    1)ViewApp class is in an exe.
    2)ViewApp class is inheriting from CFormView.
    3)ViewApp class constructor takes a resource id as parameter corresponding to the type of dialog.

    End Result: To inherit ViewDll class from ViewApp.

    Problems encountered: While instantiating ViewDll class, we get a compilation error saying that There is no default constructor available for ViewApp class".
    Hope this helps.
    LVL 49

    Accepted Solution

    It should be perfectly possible to instantiate a CView-derived object which is defined in a DLL.

    I'll have to guess that one or more of the following is true:
    o You are not exporting the class object correctly
    o You are not including the correct/required header file
    o When you instantiate the object, you are trying to do so without using required
       parameters.  The specific error is that the object does not contain a default
       constructior... so examine the constructors that are available and use one of those.

    Although this is not justified by the symptoms:
    o It is also possible that the resource is not available (MCE is looking in the DLL rather
       than the EXE or vice-versa) or the wrong resource ID is being used.  In two different
       projects it is perfectly possible to have, for instance IDR_MyDlg be equal to 123 in
       one project and equal to 321 in another.

    At this point you need to get past the compiler error, so you need to look at problems with the headers and your class object declarations and the code you use to instantiate the object.  Later, some of the other problems could come up and you can diagnose those with the debugger by breakpointing and single-stepping.

    -- Dan
    LVL 1

    Expert Comment

         Notice one thing if you are not exporting any class or a function the .lib will not be created.First check the library is exporting the required class by using __declspec(dllexport) and then try with loading of the library.

    LVL 49

    Expert Comment

    Can you describe what you did to solve this problem?  All I did was "shotgun" several potential problem areas.  It would be good for the EE database if this question contained a short description of the actual solution that you used.  Thanks!

    - Dan

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now