Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dialog from DLL function

45 views
Skip to first unread message

Pascal Steiss

unread,
Jan 13, 2009, 12:48:14 PM1/13/09
to
Dear Readers

I wrote a class, descending from CDialog.

I have a function inside a DLL, which should show this Window.

int16 ShowAdvOptions() {
CIkonAdvOptionsDlg myOptionsDlg;
myOptionsDlg.ShowWindow(SW_SHOW);
return 0;
};

This gives me an Assertion:
winocc.cpp at line 301

I suspect there is a problem since I am not using DoModal() - I don't
show the window modal since there is no parent window...

Thanks
Pascal

Ajay Kalra

unread,
Jan 13, 2009, 12:56:07 PM1/13/09
to
On Jan 13, 12:48 pm, "Pascal Steiss" <_NO-SPAM_pascal.ste...@gmx.ch>
wrote:

Problem is that you dialog object goes out of scope when you exit. You
should use new to create the object:

CIkonAdvOptionsDlg pMyOptionsDlg = new ....;

You will need to destroy the object when you close the modeless
dialog.

Also, what type of DLL is it? Unless you have specfic requirements,
you should use a MFC Extension DLL as opposed to MFC Regular DLL.

--
Ajay

Ajay Kalra

unread,
Jan 13, 2009, 12:57:53 PM1/13/09
to
On Jan 13, 12:48 pm, "Pascal Steiss" <_NO-SPAM_pascal.ste...@gmx.ch>
wrote:

In addition, you will need to create the dialog as well. This explains
it in detail:

http://www.codeproject.com/KB/dialog/gettingmodeless.aspx

--
Ajay

Norbert Unterberg

unread,
Jan 13, 2009, 1:02:27 PM1/13/09
to

Pascal Steiss schrieb:

You get the assertion because you do not create the window.
IF the dialog is a modal dialog, you must call DoModal(), even if there is no
parent window. DoModal also works if you use NULL for the parent window.

A better solution would be to change the DLL function to use a CWnd as a
parameter. You would then use that window as parent window for the dialog, like:

INT_PTR ShowAdvOptions(CWnd* parent)
{
CIkonAdvOptionsDlg myOptionsDlg(parent);
return myOptionsDlg.DoModal();
}

Norbert

Joseph M. Newcomer

unread,
Jan 13, 2009, 1:08:15 PM1/13/09
to
There are several problems here.

First, you have created a local variable to reference the dialog, so it goes out of scope
when you return from this function.

Second, you have asked to ShowWindow an object which has no window!

Third, this is in a DLL, and you have not specified the type of the DLL. If it is in a
regular DLL, you will have to use AfxManageState to make the dialog template available
when you finally get around to creating it.

Creating a C++ object to represent a window does not generally create the window, and for
CDialog-derived classes, it absolutely does not show the window.

Do you mean this to be a modal dialog or a modeless dialog? As shown, it cannot be modal
because you did not call DoModal, and it cannot be modeless because you didn't create it,
and even if you did create it, it would go out of scope when you returned.

Did you LOOK AT THE CODE? At winocc.cpp line 301? What did it say? How is it we are
supposed to help if you don't tell us what version of VS you are using (VS6, VS2002,
VS2003, VS2005, VS2008)? Line 301 can be different in each release, so giving the line
number without the version of VS is pointless. (in VS2005, for example, it is a 'else'
statement!)

The code probably complained that the HWND was NULL, which it would be, because you have
not actually created a dialog, just a C++ object that will potentially represent a dialog.
But no dialog is present.

How do you plan to access the window, given that you have no variable that represents it?

If there is no parent window, why are you trying to show a dialog? How is it you have a
GUI app that has no main window?
joe

On Tue, 13 Jan 2009 17:48:14 +0000 (UTC), "Pascal Steiss" <_NO-SPAM_pa...@gmx.ch>
wrote:

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Pascal Steiss

unread,
Jan 13, 2009, 3:00:24 PM1/13/09
to
Thanks to all for your comments.

I tried to implement it acordingly (in VS6).
It is a regular DLL - but now I added the #defines

#define _AFXDLL
#define _AFXEXT


int16 ShowAdvOptions() {
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

CIkonAdvOptionsDlg* pMyOptionsDlg = new CIkonAdvOptionsDlg(NULL);
//instead NULL: CWnd::GetDesktopWindow());
pMyOptionsDlg->Create(IDD_IKONADVOPTIONSDLG_DIALOG, NULL);
//instead NULL: CWnd::GetDesktopWindow());
pMyOptionsDlg->DoModal();

return 0;
};


Still I get a crash (I call the function in the DLL from Matlab)

If I leav away the DoModal, nothing (severe) happens.

I also tried to exchange the NULL with CWnd::GetDesktopWindow() - but
with the same result.

Do you have any further suggestions?

Pascal

Ajay Kalra

unread,
Jan 13, 2009, 3:08:19 PM1/13/09
to
On Jan 13, 3:00 pm, "Pascal Steiss" <_NO-SPAM_pascal.ste...@gmx.ch>
wrote:

You are confusing the issue. If you use DoModal, you dont need to call
Create. If you make it modeless, you will need to call Create after
creating the dialog object on the heap(creating it with new) and then
call ShowWindow.

You are mixing the two. Look at Norbert's reply for how to use it as a
modal.

Also, since you are using a Regular DLL(why?), where is your dialog
resource. Your code implies that the dialog resource is sitting in the
same DLL as the code. Is that correct? If not, you will need to use a
different version of AFX_MANAGE_STATE.

--
Ajay

AliR (VC++ MVP)

unread,
Jan 14, 2009, 11:44:56 AM1/14/09
to
One other thing instead of GetDesktopWindow why not AfxGetMainWnd?

AliR.


"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:80e9df5b-0dd4-40cd...@d36g2000prf.googlegroups.com...

Ajay Kalra

unread,
Jan 14, 2009, 1:59:05 PM1/14/09
to
On Jan 14, 11:44 am, "AliR \(VC++ MVP\)" <A...@online.nospam> wrote:
> One other thing instead of GetDesktopWindow why not AfxGetMainWnd?

Thats fine. Since OP is using a Regular DLL, AfxGetMainwnd will return
null. OP will need to call AFX_MANAGE_STATE with AppModule to get the
main window.

--
Ajay

0 new messages