CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Location
    Denmark
    Posts
    6

    Problem with Resource DLL

    Hi,
    I am trying to port my resources from a project into a resource DLL, so i can support multipla languages this way.
    I have created the dll (using Regular dll with MFC statically linked ), with the /noentry parameter.
    In my target app i use these instructions in the constructor for the app class:
    hNew = LoadLibrary( "MyDll.dll" );
    AfxSetResourceHandle( hNew );

    The dll contains all resources for the app, including dialog, bmps, and waves. How do i get the app to use the resources from the dll?

    Thanks, Michael.

  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    Exactly as for internal resources. Add
    #include "DLLresource.h"
    and, for example

    CString str;
    str.LoadString(IDS_MESSAGE), where IDS_MESSAGE is ID of string resource from DLL

  3. #3
    Join Date
    May 2002
    Location
    Denmark
    Posts
    6
    Hi, Igbrus
    Thanks for the reply.
    I have tried this change:
    //#include "resource.h" // main symbols
    #include "DllResource.h" // Dll symbols
    in the app header file.

    It seems to be working, but the moment i try something the app freezes, and i have to force it to close.

    do i miss something? Is the change of resource source not global?

    thanks, Michael

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    My suggestion is that if you need to support more than one resource handle, do *not* use MFC to do it. The reason why I state this is that the MFC functions are (believe it or not) much more harder to use than just using the Windows API functions to handle multiple resources.

    For example, when you call LoadLibrary to load the DLL, a handle is returned to you. When you now use the Windows API to (for example) call ::LoadString() (not the MFC LoadString, but the Windows API LoadString function), the first parameter is the handle of the DLL. Look at MSDN for LoadString:

    LoadString(HINSTANCE, Id, buffer, len);

    The main difference between this call and MFC is the first parameter (the last two parameters are just equivalent to what a CString would provide). Since you know the handle from the LoadLibrary call, all you have to do load a string resource from the DLL is to provide that handle as the first parameter. There is no need to be setting global variables or calling AfxWhatever() functions. If you had two or more DLL's where resources can be found, you can see the obvious advantage of doing things this way.

    On the other hand, for the MFC version of CString::LoadString(), you need to set some global resource handle before calling it. If you have two or more modules where you can get your resources, this is a nightmare to maintain correctly.

    Resource handling is one of the things that MFC does poorly, and the Windows API does very easily. If you always get your resources from your application's EXE, then the MFC resource handling is fine since almost all programs work this way. However I highly recommend doing things the "old-fashioned" Windows API way if you are handling your resources through a DLL.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2002
    Location
    Denmark
    Posts
    6
    Hi,

    Thanks for the suggestion, but can i do somthing like this with my Dialog resources, or do i have to set the text in these manualy?

    thanks, Michael Olsen

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Yes, all you need to do is to load the correct DLL, and call FindResource( ) to find the appropriate RT_DIALOG resource and LockResource() to lock the handle. Then you call (if you're using MFC) CDialog::InitModalIndirect( ) using the locked handle returned from LockResource. Then you display the dialog by calling CDialog::DoModal() (if the dialog is modal).

    I suggest you look at the functions available in the API to handle resources such as FindResource, LockResource, and creating dialogs given the dialog template (which is returned to you when you call LockResource). In MSDN there are a few articles explaining how to do this. Since you sound new to this, you should create a sample MFC app to get used to handling resources (a simple MFC app that loads one of your resource DLL's and displays one of the dialogs). Once your comfortable in handling the resources then you can move the code into your main application.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured