How do I use Resources from a Resource-Only DLL?

This is a discussion on How do I use Resources from a Resource-Only DLL? within the C++ Programming forums, part of the General Programming Boards category; Okay, I created a resource-only DLL and I am trying to make an application that can load an image from ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    How do I use Resources from a Resource-Only DLL?

    Okay, I created a resource-only DLL and I am trying to make an application that can load an image from it, such as a bitmap or icon.

    Here is my DLL code:
    Code:
    #include "stdafx.h"
    
    #ifdef _MANAGED
    #pragma managed(push, off)
    #endif
    
    BOOL APIENTRY DllMain(HMODULE hModule,
        DWORD ul_reason_for_call, LPVOID lpReserved)
        {
        return TRUE;
        }
    
    #ifdef _MANAGED
    #pragma managed(pop)
    #endif

    Then Here is the Resource file include with it:
    Code:
    ICON_MAIN ICON "icon.ico"
    This all compiles perfectly to a DLL. Then I can't seem to figure out how to load it into my project correctly.
    Here is the code I use to load the icon image:

    Code:
    const char* pFileName="resdll.dll";
    HMODULE hinstdll = LoadLibrary(pFileName);
    HICON myicon = LoadIcon(hinstdll,ICON_MAIN);
    FreeLibrary(hinstdll);
    The program compiles without error if I remove the LoadIcon line, so that is my problem I suppose. When I try to compile I get the error:
    "error C2065: 'ICON_MAIN' : undeclared identifier"
    How can I fix this?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Make sure you #include the resource file defining 'ICON_MAIN' in any file that uses it.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Please explain to me where to inlcude cuz I have no idea where.
    It is in the project so it is being compiled and what not...

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    you need to include that file in every "translation unit" BEFORE the point it is used for the first time.

    in other words:
    include the file containing SYMBOL at the beginning of all .cpp and .h files that use SYMBOL.

    if SYMBOL is currently defined in a .cpp file, either move the definition of it to a .h file (either existing .h file or new one),
    or place an extern declaration into the header.

    in other words:
    Code:
    <file "my_header.h">
    int ICON_MAIN = foo;
    
    <file "my_cpp.cpp">
    #include "my_header.h"
    or
    Code:
    <file "my_cpp.cpp">
    int ICON_MAIN = foo;
    
    <file "my_other_cpp.cpp">
    #include "my_header.h"
    
    <file "my_header.h">
    extern int ICON_MAIN;
    (where SYMBOL is ICON_MAIN in your case)

    hmmm... forget the second case, just do the first one
    Last edited by Raven Arkadon; 12-09-2005 at 07:44 PM.
    signature under construction

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Ok well now I get it to compile but the program doesn't successfully load the icon... why is this? i don't get any errors but I tried using it as the window's small icon and it doesn't display my icon, just some other default.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Raven Arkadon is correct for C/C++ files, but resource includes need to be set up slightly differently.

    In a file called, "resource.h" have the following:
    Code:
    #define ICON_MAIN 101
    The number 101 is arbitary. Each resource you add needs a define and a unique number.

    Now include this file at the top of both your .rc file and your .cpp files:
    Code:
    #include "resource.h"

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, 08:27 PM
  2. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 01:55 AM
  3. How to Creating DLL image resource
    By nostromos in forum Windows Programming
    Replies: 1
    Last Post: 10-09-2005, 03:36 AM
  4. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 03:20 AM
  5. AFX DLL with resources
    By LuckY in forum Windows Programming
    Replies: 0
    Last Post: 12-20-2003, 03:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21