Solved

Using a modeless dialog box in a DLL

Posted on 1997-08-27
6
413 Views
Last Modified: 2013-11-20
One of the functions in my DLL takes a while to execute, so I want to add a status box.  The code for the status box works great when used within an exe, but crashes when run within the DLL.

In the DLL, I end up with an access violation whenever I try to Create()
my dialog box.  My best guess is that it has something to do with the
CWnd that it's trying to use for the parent.  When I tried using the
CWnd from AfxGetMainWnd() as the parent, I get the same error.  I'm
thinking that this just means that the default parent for any dialog (if
not specified) is the main window of the app -- just what M.S. claims!

Both the application and the DLL are single threaded.

So... How do I get my modeless box to work ?

0
Comment
Question by:Srw
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
davmarc earned 100 total points
ID: 1304995
Do you create the dialog box from a dialog template included in your DLL's resources?
If so, did you remember to call AfxSetResourceHandle() passing the HINSTANCE of the DLL module?

Davide Marcato.
0
 
LVL 2

Author Comment

by:Srw
ID: 1304996
The first two lines are the ones I added...
I now get an access violation at the GetInstanceHandle();

HINSTANCE hDLL = AfxGetInstanceHandle();
AfxSetResourceHandle(hDLL);
WaitDialog* pWait = new WaitDialog();

The access violation occured at the final line in this function from Thrdcore.cpp

LRESULT CALLBACK _AfxMsgFilterHook(int code,
                                   WPARAM wParam,
                                   LPARAM lParam)
{ . . .
      return (LRESULT)AfxGetThread()->ProcessMessageFilter(code, (LPMSG)lParam);
}

0
 
LVL 4

Expert Comment

by:davmarc
ID: 1304997
Can you find where exactly the access violation occurs?

Davide Marcato.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Srw
ID: 1304998
Your last question... Where exactly is the exception? got me to thinking.  I'd been using dynamically linked MFC library, and could only get as far as my last answer showed.  So, I tried using the static linked version in the hopes that I might trace down further.  I couldn't even get it to compile -- Linker said it couldn't find '_main'.  I didn't expect to see that message from a DLL!

After a bunch of reading and looking at examples, etc, I realized that I created my DLL as a standard windows DLL, and just wedged MFC in -- all the other stuff it did worked fine...  I decided to create a small test DLL (Using the option MFC DLL) that only creates a modeless dialog.  After solving the crash due to setting the resource handle, it started working! Yea!

The one minor annoyance I encountered was getting the instance handle for my dll. If I tried
HINSTANCE inst = AfxGetInstanceHandle();
and used that, the dll couldn't find the resource.

But, if I added a DLL entry point function and saved the instance that I received, it worked fine.

So, my modeless dialog inside a dll now works!

BTW, is there a better way to get the instance handle?  It just seems slightly inelegant to do it my way.
0
 
LVL 4

Expert Comment

by:davmarc
ID: 1304999
Hmm...AfxGetInstanceHandle() does not work for your DLL, but does it work for the other small testing DLL you created?
If so, maybe you missed something more about turning your DLLinot a fully MFC compliant one.

Davide Marcato.
0
 
LVL 2

Author Comment

by:Srw
ID: 1305000
No, actually AfxGetInstanceHandle appeared to return the instance of the application both times.  The instance handle I used came from
BOOL WINAPI DllEntry(HINSTANCE hInst,
                     ULONG ul_reason_for_call,
                     LPVOID lpReserved)
{
      _dllInst = hInst;
      return TRUE;
}

And I set the linker option: /entry:"DllEntry"

I did all the DLL setup in the CWinApp derived classes's c-tor and d-tor, instead of here.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 110
Generic progress indicator 6 118
Line meaning 9 89
I could not set window to top 4 40
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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