Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. dynamic_cast and AfxGetMainWnd() failing where it shouldn't fail

dynamic_cast and AfxGetMainWnd() failing where it shouldn't fail

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Tak M
    wrote on last edited by
    #1

    I got this odd problem today (well, one of my testers got it): I got a function that returns a pointer to my main window, casted to the correct type. It looks something like this: CMyMainWnd* _GetMainWnd() { return dynamic_cast(AfxGetMainWnd()); } The strange thing is that this fails at one point. I first thought that it was due to the function being called from the wrong thread, but that wasn't the case. I took a look at what AfxGetMainWnd() returned, and it was the correct window (at least the title was correct; I never had this problem, so I can't debug it). The window title clearly states that this is "CMyMainWnd", but the dynamic cast still fails and returns 0. What could cause this problem? Maybe I should just use a stupid cast and cross my fingers ;)

    N 1 Reply Last reply
    0
    • D Daniel Tak M

      I got this odd problem today (well, one of my testers got it): I got a function that returns a pointer to my main window, casted to the correct type. It looks something like this: CMyMainWnd* _GetMainWnd() { return dynamic_cast(AfxGetMainWnd()); } The strange thing is that this fails at one point. I first thought that it was due to the function being called from the wrong thread, but that wasn't the case. I took a look at what AfxGetMainWnd() returned, and it was the correct window (at least the title was correct; I never had this problem, so I can't debug it). The window title clearly states that this is "CMyMainWnd", but the dynamic cast still fails and returns 0. What could cause this problem? Maybe I should just use a stupid cast and cross my fingers ;)

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

      D D 2 Replies Last reply
      0
      • N Neville Franks

        It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

        D Offline
        D Offline
        Daniel Tak M
        wrote on last edited by
        #3

        No, AfxGetMainWnd() does not return NULL. How else could I get the Window caption? :) The thing is that the caption is correct, just the caption of the main window as it should be. So, I assume that AfxGetMainWnd() returns the correct window pointer, but the cast just fails. And thanks for the hint on the < and >... I will keep that in mind next time.

        1 Reply Last reply
        0
        • N Neville Franks

          It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Neville Franks wrote: BTW. We can't see you You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. It looks like it is:

          return dynamic_cast<CMyMainWnd*>(AfxGetMainWnd());


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          N 1 Reply Last reply
          0
          • D David Crow

            Neville Franks wrote: BTW. We can't see you You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. It looks like it is:

            return dynamic_cast<CMyMainWnd*>(AfxGetMainWnd());


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            N Offline
            N Offline
            Nitron
            wrote on last edited by
            #5

            why dynamic_cast and not static_cast? ~Nitron.


            ññòòïðïðB A
            start

            D 1 Reply Last reply
            0
            • N Nitron

              why dynamic_cast and not static_cast? ~Nitron.


              ññòòïðïðB A
              start

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              You'd have to ask Tak that one. I was simply helping Neville see potentially hidden text.


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups