Solved

Using a modeless dialog box in a DLL

Posted on 1997-08-27
6
378 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you find where exactly the access violation occurs?

Davide Marcato.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:Srw
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

12 Experts available now in Live!

Get 1:1 Help Now