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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to change CDocument Variables from within the CWinApp derived Class?

How to change CDocument Variables from within the CWinApp derived Class?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
6 Posts 2 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.
  • F Offline
    F Offline
    Franken
    wrote on last edited by
    #1

    Hi, I've written an application (SDI), which runs a thread in my derived CWinApp class. The thread is mainly for collecting data from a RS232 Interface. Sometimes I want to assign parts of this data to variables in my document class (for storing and displaying). How can I adress these variables? I know that it somehow works with the DocTemplate pointer m_pDocManager->GetFirstDocTemplatePosition() but I don't know the correct syntax. Can anybody give me a short introduction? Thanks for your help.

    D 1 Reply Last reply
    0
    • F Franken

      Hi, I've written an application (SDI), which runs a thread in my derived CWinApp class. The thread is mainly for collecting data from a RS232 Interface. Sometimes I want to assign parts of this data to variables in my document class (for storing and displaying). How can I adress these variables? I know that it somehow works with the DocTemplate pointer m_pDocManager->GetFirstDocTemplatePosition() but I don't know the correct syntax. Can anybody give me a short introduction? Thanks for your help.

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

      The document can be accessed from anywhere like:

      AfxGetMainWnd()->GetActiveDocument();

      It can be accessed from the main application object like:

      GetMainWnd()->GetActiveDocument();


      "Take only what you need and leave the land as you found it." - Native American Proverb

      F 2 Replies Last reply
      0
      • D David Crow

        The document can be accessed from anywhere like:

        AfxGetMainWnd()->GetActiveDocument();

        It can be accessed from the main application object like:

        GetMainWnd()->GetActiveDocument();


        "Take only what you need and leave the land as you found it." - Native American Proverb

        F Offline
        F Offline
        Franken
        wrote on last edited by
        #3

        Thanks for your help. But I still have a problem. GetActiveDocument() is not a member of CWnd. With GetMainWnd() I can only access GetActiveWindow(). What is my mistake? Do I have to do a type cast? I've tried the following: CFrameWnd* pFrame = (CFrameWnd*)GetMainWnd(); CMyDocument* pDoc = (CMyDocument*)pFrame->GetActiveDocument(); pDoc->myVariable = myValue; With this code I don't get a compile error but a runtime error;

        D 1 Reply Last reply
        0
        • D David Crow

          The document can be accessed from anywhere like:

          AfxGetMainWnd()->GetActiveDocument();

          It can be accessed from the main application object like:

          GetMainWnd()->GetActiveDocument();


          "Take only what you need and leave the land as you found it." - Native American Proverb

          F Offline
          F Offline
          Franken
          wrote on last edited by
          #4

          Now it works :) somehow. The runtime error only occur if I try to place this code in the worker thread, not if it is placed somewhere else in the main application class. But I think I can find a solution for that. Thanks.

          D 1 Reply Last reply
          0
          • F Franken

            Thanks for your help. But I still have a problem. GetActiveDocument() is not a member of CWnd. With GetMainWnd() I can only access GetActiveWindow(). What is my mistake? Do I have to do a type cast? I've tried the following: CFrameWnd* pFrame = (CFrameWnd*)GetMainWnd(); CMyDocument* pDoc = (CMyDocument*)pFrame->GetActiveDocument(); pDoc->myVariable = myValue; With this code I don't get a compile error but a runtime error;

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

            Franken wrote:

            GetActiveDocument() is not a member of CWnd.

            Right, it's part of the frame.

            Franken wrote:

            With GetMainWnd() I can only access GetActiveWindow().

            Not necessary in this case.

            Franken wrote:

            With this code I don't get a compile error but a runtime error;

            Which is?


            "Take only what you need and leave the land as you found it." - Native American Proverb

            1 Reply Last reply
            0
            • F Franken

              Now it works :) somehow. The runtime error only occur if I try to place this code in the worker thread, not if it is placed somewhere else in the main application class. But I think I can find a solution for that. Thanks.

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

              Franken wrote:

              Now it works somehow.

              This is usually a good indication that the problem still exists and has simply been masked or moved to a new location.

              Franken wrote:

              The runtime error only occur if I try to place this code in the worker thread,

              Which makes sense as worker threads shouldn't be accessing such information. See these two articles for more on threads and how they are supposed to communicate back and forth with the primary thread.* http://www.flounder.com/workerthreads.htm

              • http://www.flounder.com/uithreads.htm


                "Take only what you need and leave the land as you found it." - Native American Proverb

              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