Solved

Using a modeless dialog box in a DLL

Posted on 1997-08-27
6
405 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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