Thread: DLL with resources

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    DLL with resources

    Okay before you go thinking this is a repost of the same question, know it's not. I would like to know if you can tell me how to go about creating a DLL in MSVC that contains resources and CDialog derived classes.

    If you know anything on the topic, let me know. Thanks Charlie.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Check out MindCracker for examples.

    http://www.mindcracker.com/mindcracker/c_cafe/dll.asp

    Kuphryn

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    VC++ 6.0:
    - File->New, "Projects" tab, "Win32 Dynamic-Link Library"
    - select "A simple DLL project"
    - File->New, "Files" tab, "Resource Script"
    - Insert->Resource..., select Icon or Dialog

    Use LoadLibrary() to map the DLL into your address space.
    Use LoadIcon() to access icons from the DLL.
    Use LoadResource() to bring a dialog template into global memory, so you can use it with CreateDialogIndirect() or DialogBoxIndirect()

    gg

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Thank you for the suggestions.

    Kuphryn, that was a good link, but the info is no good. The version of VC that fellow uses is completely different from ver6. I cannot create a dll the way he illustrates. Aside from that I can't get his sample app to compile.

    Codeplug, I'm sure that would work. Thanks for explaining however I would like to have a dlg that I can create simply by calling a function in the dll. For example, in the dll's .h:

    void initDlg(HWND parent);

    Then in the app:

    initDlg(GetSafeHwnd());

    I am successful with doing all this but only until I try to add a CPropertySheet to my dlg then it crashes... There must be something I'm missing, but I don't know what.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I missed the "using MFC" part....

    Try putting "_AFXDLL" in
    Project -> Settings (All Configurations) -> "C/C++" tab -> Category = "Preprocessor" -> "Preprocessor Definitions:"
    This is a comma seperated list.

    This will tell MFC to look for resources in all modules loaded in your address space, and not just the resources in the EXE that's running.
    If that doesn't work, then find out where/why you're crashing using the debugger.

    [EDIT] assuming you are linking with MFC DLL dynamically[/EDIT]

    gg
    Last edited by Codeplug; 12-22-2003 at 05:44 PM.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I want to kill myself trying to make this thing work.

    Adding _AFXDLL required me to add the switch /MD. Done. Then it compiles but running the app with the dll doesn't work (ordinal 5076 in MFC42D.dll is missing)... So I gave up on trying that. I've trimmed the whole thing down into a very simple DLL and app and it runs and the dll dialog opens as it should, but then it crashes citing user.exe.

    I'm attaching the simplified version of this crap. Please take a look at it and tell me if you can figure out what the deal is. It is too simple, so it's got to be something minor (I hope). Two projects are included: theDLL (which creates the .lib and .dll) and theAPP (which uses them). Just compile theDLL then compile+run theAPP...

    Muchas gracias amigos.

    [edit]removed attachment[/edit]
    Last edited by LuckY; 12-23-2003 at 10:34 AM.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    AHA! I've FINALLY figured it out (thanks to google of course)...

    In case you're wondering:

    file:new:projects-MFC AppWizard (dll):finish(accept defaults)

    do normal dialog stuff. put any free function implementations in the dll's .cpp file and the function name in the .def file. Most importantly the first line of any free function implementation must be: AFX_MANAGE_STATE(AfxGetStaticModuleState());

    ie, in MainDllFile.cpp append:
    Code:
    void doDialog() {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
      CTheDialog dlg;
      dlg.DoModal();
    }
    in MainDllFile.def:
    Code:
        ; Explicit exports can go here
        doDialog
    then in the app using the dll, #include a file that contains the following or just type it into the files that use the func:
    extern void doDialog();
    ==or==
    __declspec(dllimport) void doDialog();
    Then you can call it. Alternately you can exlude the .def entry and just use a header that both the dll and app can include which contains something like:
    Code:
    #ifndef _EXPORT_H_
    #define _EXPORT_H_
    
    #ifdef THE_DLL// the dll should define this before inclusion
    #define DLLEXPORT __declspec(dllexport)
    #else
    #define DLLEXPORT __declspec(dllimport)
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    DLLEXPORT void doDialog();
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif //_EXPORT_H_

    That's it (yeah, after 4 days of trying)!!!
    Last edited by LuckY; 12-23-2003 at 11:54 AM.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Wow, 3 posts in a row ;P

    I just wanted to include a simple sample app. It will be helpful if anyone else is ever looking for a solution to an MFC DLL with AFX or ordinal errors with MFC42d.dll (just throwin in some keywords).

    Just compile AfxWinDll then compile+run test.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's a technical article that will tie up any loose ends on using MFC in DLL's.

    Yeah, you should use the appwizards whenever possible.
    I downloaded you're code before you removed it - this is what you would have needed to get that code to work:

    "theAPP" Project Settings: (Release)
    >> C/C++ tab -> Preprocessor -> Remove "_AFXDLL" from Preprocessor definitions (I think you added it from my previous post but that was meant for the DLL project, however when you select Static or Dynamic MFC linkage, it's smart enough to add/remove _AFXDLL from the preprocessor definitions for DLL projects)

    "theDLL" Project Settings: (all configurations)
    >> General tab -> "Use MFC in a Static Library"
    >> Added the following code to MainDlg.cpp
    Code:
    // This should look familiar now that you've done the MFC Appwizard (DLL) :)
    class CTestDLLApp : public CWinApp
    {
    public:
        CTestDLLApp() {}
        DECLARE_MESSAGE_MAP()
    };
    
    BEGIN_MESSAGE_MAP(CTestDLLApp, CWinApp)
    END_MESSAGE_MAP()
    
    CTestDLLApp theApp; // absence of this was the root cause of your exceptions
    In this example, since "theDLL" is linking with MFC statically, you don't need to call AFX_MANAGE_STATE().
    If you link to MFC dynamically, then you do need to call it.

    Also, it's a good idea not to pass any MFC objects in or out of the DLL since you can't gaurantee that the version of the MFC on both ends with be the same.

    Change this:
    >> THEDLL_API void doDialog(CWnd*);
    to
    >> THEDLL_API void doDialog(HWND);
    or
    >> THEDLL_API void doDialog(void);

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL resources with, image files?
    By parad0x13 in forum C++ Programming
    Replies: 0
    Last Post: 03-15-2009, 07:27 PM
  2. How do I use Resources from a Resource-Only DLL?
    By FWGaming in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2005, 10:14 PM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. AFX DLL with resources
    By LuckY in forum Windows Programming
    Replies: 0
    Last Post: 12-20-2003, 03:51 PM