Solved

Using a modeless dialog box in a DLL

Posted on 1997-08-27
6
412 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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
repeateFront java challenge 31 94
repeatSeparator  java  challenge 13 58
Separate into files by filename 12 87
sumNumber challenge 16 136
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

791 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