Solved

Loading a menu resource from a dll

Posted on 2002-06-28
4
186 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 85
Get filename and folder into excel 7 67
sumNumber challenge 16 99
move a line in eclipse 3 60
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now