?
Solved

Loading a menu resource from a dll

Posted on 2002-06-28
4
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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