Board index » Visual Studio » verify that a dialog box exists

verify that a dialog box exists

Visual Studio361
I want to verify that a dialog box exists before I send it some message. I

have a pointer to its object.

Can someone remind me please?



Thanks



George


-
 

Re:verify that a dialog box exists

Quote
I want to verify that a dialog box exists before I send it some message. I

have a pointer to its object.

Can someone remind me please?





if(pDlg && pDlg->m_hWnd)

{

...



}



---

Ajay



-

Re:verify that a dialog box exists

Quote
I want to verify that a dialog box exists before I send it some message. I

have a pointer to its object.

Can someone remind me please?





You can use ASSERT_VALID as well. It works in Debug only but its of

great use. Something like:



ASSERT_VALID(pDlg);

if(pDlg)

{



}



---

Ajay



-

Re:verify that a dialog box exists

You can use GetSafeHwnd() to check it out if you have the pointer:



msdn2.microsoft.com/en-us/library/d64ehwhz.aspx">msdn2.microsoft.com/en-us/library/d64ehwhz.aspx



Unfortuantely if the pointer is not really for a dialog you may get an

exception. You could set the pointer to NULL when it is deleteted just so

you could check that first.



Tom





"abi" <Abi@john.com>wrote in message

Quote
I want to verify that a dialog box exists before I send it some message. I

have a pointer to its object.

Can someone remind me please?



Thanks



George







-

Re:verify that a dialog box exists

I presume you are talking here about a modeless dialog. If the modeless dialog has a

pointer, then you should assume it exists. If it can be destroyed, then it must undertake

to notify its parent that it has been destroyed, so the parent can set that pointer back

to NULL. Otherwise, you will be relying on a variety of undocumented and unsupported

kinds of behavior, so it is not recommended.

joe



On Thu, 31 Aug 2006 20:07:16 -0700, "abi" <Abi@john.com>wrote:



Quote
I want to verify that a dialog box exists before I send it some message. I

have a pointer to its object.

Can someone remind me please?



Thanks



George



Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:verify that a dialog box exists

Or

if(pDlg->GetSafeHwnd() != NULL)



joe



On 31 Aug 2006 10:18:45 -0700, "Ajay Kalra" <ajaykalra@yahoo.com>wrote:



Quote
>I want to verify that a dialog box exists before I send it some message. I

>have a pointer to its object.

>Can someone remind me please?

>



if(pDlg && pDlg->m_hWnd)

{

...



}



---

Ajay

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:verify that a dialog box exists





"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

Quote
Or

if(pDlg->GetSafeHwnd() != NULL)





This can blow up if pDlg is NULL.



--

Ajay Kalra [MVP - VC++]

ajaykalra@yahoo.com





-

Re:verify that a dialog box exists

"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message

Quote




"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

news:ml2hf2h1uh9421svql6nlh75phis3pr9pi@4ax.com...

>Or

>if(pDlg->GetSafeHwnd() != NULL)

>



This can blow up if pDlg is NULL.





No, Joe is right: GetSafeHwnd() returns NULL if the CWnd is not attached to

a window or if it is used with a NULL CWnd pointer. (from MSDN)



-- David





-

Re:verify that a dialog box exists

"David Ching" <dc@remove-this.dcsoft.com>wrote in message

Quote
"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message

news:eqnonGkzGHA.3280@TK2MSFTNGP02.phx.gbl...

>

>

>"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

>news:ml2hf2h1uh9421svql6nlh75phis3pr9pi@4ax.com...

>>Or

>>if(pDlg->GetSafeHwnd() != NULL)

>>

>

>This can blow up if pDlg is NULL.

>



No, Joe is right: GetSafeHwnd() returns NULL if the CWnd is not attached

to

a window or if it is used with a NULL CWnd pointer. (from MSDN)





This will blow up:



CWnd* pDlg = NULL;

if(pDlg->whateverFunction() )

{



}





--

Ajay Kalra [MVP - VC++]

ajaykalra@yahoo.com





-

Re:verify that a dialog box exists

"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message



Quote
This will blow up:



CWnd* pDlg = NULL;

if(pDlg->whateverFunction() )

{

}





It won't blow up if whateverFunction():



a) is not virtual

b) does not reference member data





-- David





-

Re:verify that a dialog box exists

Actually, it can't, and that's why it's called GetSafeHwnd(), with emphasis on the "Safe";

read the code.



From afxwin2.inl:



_AFXWIN_INLINE HWND CWnd::GetSafeHwnd() const

{ return this == NULL ? NULL : m_hWnd; }



joe

On Fri, 1 Sep 2006 23:41:21 -0400, "Ajay Kalra" <ajaykalra@yahoo.com>wrote:



Quote




"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

news:ml2hf2h1uh9421svql6nlh75phis3pr9pi@4ax.com...

>Or

>if(pDlg->GetSafeHwnd() != NULL)

>



This can blow up if pDlg is NULL.

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:verify that a dialog box exists

No. In fact, there was a question earlier in some thread where someone was asking why his

code didn't blow up. It turns out that whateverFunction() did not actually do anything

involving 'this'. All that happens is that 'this' is NULL. The blowup happens not at the

call, but at the access to the 'this' pointer (except for virtual methods, that will die

instantly when you try to access their vtable pointer via the NULL pointer).



Example:



void whateverFunction()

{

if(this == NULL)

return;

... do something

}



would actually work robustly, although I think something like this is a little marginal

with respect to the actual C++ standard.

joe

On Sat, 2 Sep 2006 00:14:59 -0400, "Ajay Kalra" <ajaykalra@yahoo.com>wrote:



Quote
"David Ching" <dc@remove-this.dcsoft.com>wrote in message

news:en7Kg.5569$yO7.3182@newssvr14.news.prodigy.com...

>"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message

>news:eqnonGkzGHA.3280@TK2MSFTNGP02.phx.gbl...

>>

>>

>>"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

>>news:ml2hf2h1uh9421svql6nlh75phis3pr9pi@4ax.com...

>>>Or

>>>if(pDlg->GetSafeHwnd() != NULL)

>>>

>>

>>This can blow up if pDlg is NULL.

>>

>

>No, Joe is right: GetSafeHwnd() returns NULL if the CWnd is not attached

to

>a window or if it is used with a NULL CWnd pointer. (from MSDN)





This will blow up:



CWnd* pDlg = NULL;

if(pDlg->whateverFunction() )

{



}

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:verify that a dialog box exists

"David Ching" <dc@remove-this.dcsoft.com>wrote in message

Quote
"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message

news:uhONaZkzGHA.744@TK2MSFTNGP05.phx.gbl...



>This will blow up:

>

>CWnd* pDlg = NULL;

>if(pDlg->whateverFunction() )

>{

>}

>



It won't blow up if whateverFunction():



a) is not virtual

b) does not reference member data





Would you put this code in production?



---

Ajay









-

Re:verify that a dialog box exists







"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

Quote
No. In fact, there was a question earlier in some thread where someone

was asking why his

code didn't blow up. It turns out that whateverFunction() did not

actually do anything

involving 'this'. All that happens is that 'this' is NULL. The blowup

happens not at the

call, but at the access to the 'this' pointer (except for virtual methods,

that will die

instantly when you try to access their vtable pointer via the NULL

pointer).



Example:



void whateverFunction()

{

if(this == NULL)

return;

... do something

}



would actually work robustly, although I think something like this is a

little marginal

with respect to the actual C++ standard.



Code should still be changed to check for non-Nullness of CWnd. I am going

to try this out.



--

Ajay Kalra [MVP - VC++]

ajaykalra@yahoo.com







-

Re:verify that a dialog box exists

"Ajay Kalra" <ajaykalra@yahoo.com>wrote in message

Quote
Would you put this code in production?





No, but it won't blow up either. The only reason to avoid it is to protect

yourself from what happens if the implementation of whateverFunction() were

changed to use "this".



But, in this PARTICULAR example,



I would use either:



if ( pDlg && pDlg->m_hWnd )



Or



if ( pDlg->GetSafeHwnd() )





because GetSafeHwnd() was invented exactly for the purpose of not having to

check for NULL pDlg. What you did:



if ( pDlg && pDlg->GetSafeHwnd() )





is not optimal.





Not that all this means much. We have too much time on our hands, and on a

Saturday to boot! ;)





-- David





-

Re:verify that a dialog box exists

GetSafeHwnd() already does that...

joe



On Sat, 2 Sep 2006 16:16:30 -0400, "Ajay Kalra" <ajaykalra@yahoo.com>wrote:



Quote






"Joseph M. Newcomer" <newcomer@flounder.com>wrote in message

news:nvgjf2ljrr2lpc5a5bvd1dn79buv61impv@4ax.com...

>No. In fact, there was a question earlier in some thread where someone

was asking why his

>code didn't blow up. It turns out that whateverFunction() did not

actually do anything

>involving 'this'. All that happens is that 'this' is NULL. The blowup

happens not at the

>call, but at the access to the 'this' pointer (except for virtual methods,

that will die

>instantly when you try to access their vtable pointer via the NULL

pointer).

>

>Example:

>

>void whateverFunction()

>{

>if(this == NULL)

>return;

>... do something

>}

>

>would actually work robustly, although I think something like this is a

little marginal

>with respect to the actual C++ standard.



Code should still be changed to check for non-Nullness of CWnd. I am going

to try this out.

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-