Solved

Loading a menu resource from a dll

Posted on 2002-06-28
4
191 Views
Last Modified: 2013-11-20
Well, it's Friday and it's late, and my thinking is not getting better, so I thought I might as well post a question and call it a day.

I have defined a simple menu within a window. Previously, my window class was the main application and was compiled to an executable. At that time, the menu showed up fine, no problems. However, I recently made a dll out of it and use it from another main application. All non-resource based code is still perfectly fine, but the menu is nowherre to be seen.

I made a small test project to test my hypothesis that it truly is some problem with the code finding the resource when it is a dll, and it turned out the same way. If I run it as a single executable, the menu works fine, but when I make a dll out of it and try to access it as a dll, the menu won't load (LoadMenu returns 0, which is a good indication that something is not quite right).

Does anybody know what I'm doing wrong? The menu is the only resource in my entire project, but I suspect I'd have problems with any type of resource. I guess I've really messed up at some point...

Any help is greatly appriciated!
0
Comment
Question by:IFKMild
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:migel
ID: 7116956
Hi!
You need to set proper resource handle to access DLL resource. Try use AfxSetResourceHandle(hDLLInstance);
where hDLLInstance - your DLL instance.
code can look like this:
HINSTANCE hInstOld = AfxSetResourceHandle(hDLLInstance);

...LoadMenu(...);
AfxSetResourceHandle(hInstOld);
0
 

Author Comment

by:IFKMild
ID: 7121490
Ok, I think you're on to something here, but unfortunately I still can not get it quite right.

I should make it clear that I'm not trying to access a resource outside of my dll. My app is (at this time) just a small test application that only instantiates my window class. The window class (which is exported in a dll) in itself tries to load the resource, but is unable to do so.

I tried setting the resource handle with AfxSetResourceHandle, but would this really help me. I guess it doesn't make sense to set it within the dll itself, and since I really don't try to access the resource outside of my dll, there should be no need to set the resource handle for the app, or am I wrong?

I'll try to make things a little clearer:

I have two projects:
* "CTestWindow", which displays a menu. This ran fine as a exe, and now I've converted it into a regular DLL using shared MFC. I export the window class.
* "CTestApp" which is a minimal app that only creates an instance of the exported window class.

As stated, the window shows up just fine, but it just won't find any resources. As you said, this probably has to do with me not having set the resourcehandle properly, but I did not quite understand where and how to set it.

I guess it must be set in the CTestWindow class, but where do I find the actual handle of the DLL. I tried to do HINSTANCE handle = AfxGetInstanceHandle() and then AfxSetResourceHandle( handle ) within the CTestWindow class, but it didn't really make sense, and it didn't work.

I know I've missed out on something vital here as I must confess MFC and DLL:s are not my primary competence area, so any further help would be really appriciated.

Cheers,

Christian
0
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 7121532
Hi!
You are wrong...
1. Your technique work for NON MFC applications. but for MFC it is not working since MFC classes assume that there is one resource source - main APP.
Since your DLL want use our private resources not APP`s one you have to tell MFC library that it must search in the your DLL not in the main APP module.
AfxSetResourceHandle successefuly make this.
2. To get DLL instance handle you can store it in the your DLL DllMain entry point
0
 

Author Comment

by:IFKMild
ID: 7121638
Ok. Now I understand what is happening. I think I'll use the AFX_MANAGE_STATE(AfxGetStaticModuleState()) which will do the trick for me at this point.

Thank you for your input. Points are heading your way.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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