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

dynamic_cast<CStatic*>(GetDlgItem(...)) => 0 but (CStatic*)GetDlgItem(...) works

293 views
Skip to first unread message

Bill Davy

unread,
Oct 16, 2008, 12:19:25 PM10/16/08
to
{

CWnd* p1 = GetDlgItem(IDC_STATICABOUTVERSION);

CStatic *p2 = dynamic_cast<CStatic*>(p1);

LOGINFO((void*)p1 << " => " << (void*)p2);

}

17:15:34.792 - INFO - CAboutDlg::OnInitDialog() - 016B46A8 => 00000000

Any thoughts?

Bill

Out until Monday - hooray!


AliR (VC++ MVP)

unread,
Oct 16, 2008, 12:38:11 PM10/16/08
to
Turn on RTTI, under the C/C++/Language settings.

AliR.


"Bill Davy" <Bi...@XchelSys.co.uk> wrote in message
news:uoK%232r6LJ...@TK2MSFTNGP02.phx.gbl...

Mark Salsbery [MVP]

unread,
Oct 16, 2008, 12:40:46 PM10/16/08
to
"Bill Davy" <Bi...@XchelSys.co.uk> wrote in message
news:uoK#2r6LJH...@TK2MSFTNGP02.phx.gbl...

> {
>
> CWnd* p1 = GetDlgItem(IDC_STATICABOUTVERSION);
>
> CStatic *p2 = dynamic_cast<CStatic*>(p1);
>
>
>
> LOGINFO((void*)p1 << " => " << (void*)p2);
>
> }
>
>
>
> 17:15:34.792 - INFO - CAboutDlg::OnInitDialog() - 016B46A8 => 00000000
>
>
>
> Any thoughts?

The CStatic version will only work if there's a CStatic or derived object
wrapping the control's HWND somewhere.

If there's no CStatic or derived object, then MFC creates a temporary CWnd
object, which can't be dynamically cast to a CStatic.

Why not just add a CStatic variable to your class instead of using
GetDlgItem()?

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

Scott McPhillips [MVP]

unread,
Oct 16, 2008, 12:46:03 PM10/16/08
to
GetDlgItem operates outside of (beneath) the C++ level. It does not return
a CStatic* even if the control is a static control. There is no CStatic
object associated with the control. Therefore dynamic_cast is expected to
fail here. If you use static_cast you can use the returned pointer, not
because there is a CStatic but because the memory layout of a CWnd is
identical to the memory layout of a CStatic. In other words, by the rules
of C++ it is cheating, but it works anyway.

This may be hard to understand. That's one reason (of many) that GetDlgItem
is best avoided. Use the IDE to create control member variables instead!


"Bill Davy" <Bi...@XchelSys.co.uk> wrote in message

news:uoK%232r6LJ...@TK2MSFTNGP02.phx.gbl...

--
Scott McPhillips [VC++ MVP]

Joseph M. Newcomer

unread,
Oct 16, 2008, 2:57:34 PM10/16/08
to
See below...

On Thu, 16 Oct 2008 17:19:25 +0100, "Bill Davy" <Bi...@XchelSys.co.uk> wrote:

> {
>
> CWnd* p1 = GetDlgItem(IDC_STATICABOUTVERSION);
>
> CStatic *p2 = dynamic_cast<CStatic*>(p1);
>
>
>
> LOGINFO((void*)p1 << " => " << (void*)p2);
>
> }
>
>
>
>17:15:34.792 - INFO - CAboutDlg::OnInitDialog() - 016B46A8 => 00000000
>
>
>
>Any thoughts?
>

****
Yes. Stop using GetDlgItem. It is essentially obsolete for common programming. It is
clear from this usage that it is completely inappropriate; create a class member and just
use it. Don't waste your time trying to use MFC as if it is bare Win32 with no support.
Use it correctly. Forget you ever heard of GetDlgItem; if you are writing more than once
a year, you are not using MFC correctly.

I would write this simply as
c_AboutVersion.SetWindowText(...something useful here...);
you have taken three overly complex lines to accomplish a trivial one-line task.
joe
****


>
>
>Bill
>
>Out until Monday - hooray!
>

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

Doug Harrison [MVP]

unread,
Oct 16, 2008, 9:23:10 PM10/16/08
to
On Thu, 16 Oct 2008 09:40:46 -0700, "Mark Salsbery [MVP]"
<MarkSalsbery[MVP]@newsgroup.nospam> wrote:

>The CStatic version will only work if there's a CStatic or derived object
>wrapping the control's HWND somewhere.
>
>If there's no CStatic or derived object, then MFC creates a temporary CWnd
>object, which can't be dynamically cast to a CStatic.
>
>Why not just add a CStatic variable to your class instead of using
>GetDlgItem()?

Correct on all accounts, and as AliR mentioned, RTTI must be enabled for
dynamic_cast to work.

--
Doug Harrison
Visual C++ MVP

Bill Davy

unread,
Oct 20, 2008, 3:09:20 AM10/20/08
to

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:gf3ff4pjb6ee37cgs...@4ax.com...

Wow, just explored add member. I'm going to spend a few days removing all
trace of GetDlgItem(). Wish the documentation mentioned it was not the way
to go.

Many thanks to you all for your excellent advice.

Bill


0 new messages