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. How to get handle to main frame in CDocument

How to get handle to main frame in CDocument

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 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.
  • G Offline
    G Offline
    GameProfessor
    wrote on last edited by
    #1

    hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?

    B K 2 Replies Last reply
    0
    • G GameProfessor

      hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?

      B Offline
      B Offline
      bob16972
      wrote on last edited by
      #2

      MFC Doc/View: How to obtain a pointer to various objects?[^]

      G 1 Reply Last reply
      0
      • G GameProfessor

        hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?

        K Offline
        K Offline
        kanduripavan
        wrote on last edited by
        #3

        use AfxGetMainWnd();and typecast to the mainframe pointer. I dont know if it works or not just give a try.if its not working we will try again.

        1 Reply Last reply
        0
        • B bob16972

          MFC Doc/View: How to obtain a pointer to various objects?[^]

          G Offline
          G Offline
          GameProfessor
          wrote on last edited by
          #4

          thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why

          B H 2 Replies Last reply
          0
          • G GameProfessor

            thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #5

            I took an old multithreaded app and gutted it to use AfxGetMainWnd() to populate a member variable in the thread and the CMainFrame is receiving the messages without any apparent problems. The message was registered with RegisterWindowMessage(). All the thread creation code is consistent as your snippet so I'm not sure what it is you are fighting. Do you have any other details that might help us identify where our code differs?

            G 1 Reply Last reply
            0
            • B bob16972

              I took an old multithreaded app and gutted it to use AfxGetMainWnd() to populate a member variable in the thread and the CMainFrame is receiving the messages without any apparent problems. The message was registered with RegisterWindowMessage(). All the thread creation code is consistent as your snippet so I'm not sure what it is you are fighting. Do you have any other details that might help us identify where our code differs?

              G Offline
              G Offline
              GameProfessor
              wrote on last edited by
              #6

              I've found the problem AfxGetMainWnd() returns 0 in CMyDoc::OnNewDocument() AfxGetMainWnd() only return after all MainFrame, Doc, View have finished initialize. So to get the correct pointer, I must put it earliest in the end of InitInstance and it work.

              B 1 Reply Last reply
              0
              • G GameProfessor

                thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why

                H Offline
                H Offline
                Hans Dietrich
                wrote on last edited by
                #7

                Try passing the HWND to the thread instead of the CWnd pointer. You can get the HWND by doing something like m_pX25Thread->m_hWnd = m_pMyWnd->m_hWnd;. Best wishes, Hans

                1 Reply Last reply
                0
                • G GameProfessor

                  I've found the problem AfxGetMainWnd() returns 0 in CMyDoc::OnNewDocument() AfxGetMainWnd() only return after all MainFrame, Doc, View have finished initialize. So to get the correct pointer, I must put it earliest in the end of InitInstance and it work.

                  B Offline
                  B Offline
                  bob16972
                  wrote on last edited by
                  #8

                  That would make sense. I had put my code in a button click handler for the view so I guess I didn't pay enough attention to where your code was being placed versus the code itself.:-O

                  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