Load resources such as images and icons in my .dll project

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
sohaibimran
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Sep 29, 2020 10:34 pm

Load resources such as images and icons in my .dll project

Post by sohaibimran »

I am a beginner in wxWidgets. I am using windows and visual studio for development of my applications. Recently I was asked to convert my .exe application to a dll so that another .exe can load it as a plugin.
The application that I made had an icon and various buttons that had loaded bitmaps to them. All the bitmaps were set using resources.rc file where I declared all the bitmaps in resources like this

Code: Select all

application            ICON        "application.ico"
button1                ICON        "button1.bmp"
and in my code where I set these

Code: Select all

btnMyButton->SetBitmap(wxICON(button1));
The application loaded all the icons correctly in the .exe of the app. But when I converted my application to a dll (to be used as a plugin) all the icons were disappeared. My buttons did not load the bitmaps. My frame was icon less. In the host .exe the error message was
Can't load bitmap 'button1.bmp' from resources! Check .rc file
So my question is

How can we include the resources in the dll so that when it is loaded as plugin from a different .exe it loads all the icons and bitmaps if any correctly.

edit: My bad just missed a 1 while typing.
Last edited by sohaibimran on Thu Apr 08, 2021 5:15 am, edited 1 time in total.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4182
Joined: Sun Jan 03, 2010 5:45 pm

Re: Load resources such as images and icons in my .dll project

Post by PB »

1. Are the resources in the DLL? You should be able to include them in the same way you do for an executable.

2. Assuming they are there, AFAICT, loading an icon from uses wxGetInstance():
https://github.com/wxWidgets/wxWidgets/ ... e.cpp#L534
Perhaps the instance returned by wxGetInstance() is that of an executable and not the DLL?

That being said, if the latter is the root of the issue, I had no idea about a simple workaround it with wxIcon. Perhaps using wxIconBundle instead when one specify the HINSTANCE or using wxIconLocation with wxIcon?

But I guess someone else would know the correct solution.
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Load resources such as images and icons in my .dll project

Post by doublemax »

Code: Select all

button                 ICON        "button1.bmp"

Code: Select all

btnMyButton->SetBitmap(wxICON(button1));
Is this from real code?

Try:

Code: Select all

btnMyButton->SetBitmap(wxICON(button));
Use the source, Luke!
sohaibimran
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Sep 29, 2020 10:34 pm

Re: Load resources such as images and icons in my .dll project

Post by sohaibimran »

@doublemax I just missed a 1 while typing. I have edited my original post. Thanks for pointing
sohaibimran
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Sep 29, 2020 10:34 pm

Re: Load resources such as images and icons in my .dll project

Post by sohaibimran »

PB wrote: Wed Apr 07, 2021 12:50 pm 1. Are the resources in the DLL? You should be able to include them in the same way you do for an executable.

2. Assuming they are there, AFAICT, loading an icon from uses wxGetInstance():
https://github.com/wxWidgets/wxWidgets/ ... e.cpp#L534
Perhaps the instance returned by wxGetInstance() is that of an executable and not the DLL?

That being said, if the latter is the root of the issue, I had no idea about a simple workaround it with wxIcon. Perhaps using wxIconBundle instead when one specify the HINSTANCE or using wxIconLocation with wxIcon?

But I guess someone else would know the correct solution.
On point 1: Yes I have a .rc file that has relative path to the icons and images... In the .exe output of the same project every resources is loaded fine. But when I convert it to a plugin (.dll) nothing is loaded. I have tried putting resources in the same folder as the dll but it did not work. What could possibly be going wrong? Is there any other way to add resources when making a plugin (.dll)
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4182
Joined: Sun Jan 03, 2010 5:45 pm

Re: Load resources such as images and icons in my .dll project

Post by PB »

If it is what I suspect (i.e., wrong HINSTANCE used), the only workarounds when using Windows resources are those two I mentioned before. Of course, one still use Win32 API directly, using the DLL's HINSTANCE.

I would ask in the wx-users what is the optimal solution, as it seems this scenario should not be that uncommon.
sohaibimran
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Sep 29, 2020 10:34 pm

Re: Load resources such as images and icons in my .dll project

Post by sohaibimran »

PB wrote: Thu Apr 08, 2021 6:18 am If it is what I suspect (i.e., wrong HINSTANCE used), the only workarounds when using Windows resources are those two I mentioned before. Of course, one still use Win32 API directly, using the DLL's HINSTANCE.

I would ask in the wx-users what is the optimal solution, as it seems this scenario should not be that uncommon.
Thanks for the reply.

One more thing is there any link or wiki source to adding resources to visual studio project when making a .dll
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4182
Joined: Sun Jan 03, 2010 5:45 pm

Re: Load resources such as images and icons in my .dll project

Post by PB »

sohaibimran wrote: Thu Apr 08, 2021 9:04 am One more thing is there any link or wiki source to adding resources to visual studio project when making a .dll
I do not think that the procedure is different from doing that for an executable: Do you have a reason to believe otherwise? Moreover, adding the resources is not an issue here, is it? You can easily verify whether a resource is present in a DLL using Resource Hacker.

TBH, I am not sure if you understand what I am trying to say about all that instance and resources stuff.
sohaibimran
Earned a small fee
Earned a small fee
Posts: 13
Joined: Tue Sep 29, 2020 10:34 pm

Re: Load resources such as images and icons in my .dll project

Post by sohaibimran »

TBH, I am not sure if you understand what I am trying to say about all that instance and resources stuff.
You are right as I am a beginner I don't have any clue about wxGetInstance and win32 API.
stahta01
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 548
Joined: Fri Nov 03, 2006 2:00 pm

Re: Load resources such as images and icons in my .dll project

Post by stahta01 »

Did you check the simple things like making sure wxHAS_IMAGES_IN_RESOURCES is defined?

Note: If you do not know how to check that; I suggest just replacing wxICON(sample) with wxIcon("sample") and seeing if it works.
If it does then you have a define issue building the project.

Edit: Based on your error message this is not likely to be the problem.

Tim S.

From wx/gdicmn.h

Code: Select all

#if defined(__WINDOWS__) && wxUSE_WXDIB
    #define wxHAS_IMAGES_IN_RESOURCES
#endif

/* Useful macro for creating icons portably, for example:

    wxIcon *icon = new wxICON(sample);

  expands into:

    wxIcon *icon = new wxIcon("sample");      // On Windows
    wxIcon *icon = new wxIcon(sample_xpm);    // On wxGTK/Linux
 */

#ifdef wxHAS_IMAGES_IN_RESOURCES
    // Load from a resource
    #define wxICON(X) wxIcon(wxT(#X))
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Load resources such as images and icons in my .dll project

Post by ONEEYEMAN »

Hi,
Can you show us what is your DllMain looks like?
Do you create an instance of wxApp inside the DLL?

Thank you.
Post Reply