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. IsWindow(m_hwnd) returns 0.

IsWindow(m_hwnd) returns 0.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
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.
  • S Offline
    S Offline
    suninwater
    wrote on last edited by
    #1

    IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA

    D M S 3 Replies Last reply
    0
    • S suninwater

      IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA

      D Offline
      D Offline
      Dave Bryant
      wrote on last edited by
      #2

      The remarks in MSDN for IsWindow() actually say that a thread should not call IsWindow() for a window that it did not create because the window could be destroyed after the function is called - so that's not a promising start... Anyway, here's a couple of likely reasons for your problem: 1) Does the call to CreateWindow() succeed? If not, then obviously IsWindow() will not succeed either. 2) When does the window get destroyed? If it is destroyed in the MyClass destructor, then this will be called at the end of the scope of the MyClass object (since it is created on the stack). From the code snippet above, that means that it will be called when mainfunc() terminates, which, unless you have some sort of syncrhonisation, will be before your thread function terminates. Consequently, the window will be destroyed before the thread gets around to using it. Dave http://www.cloudsofheaven.org

      S 1 Reply Last reply
      0
      • S suninwater

        IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA

        M Offline
        M Offline
        MultiThread
        wrote on last edited by
        #3

        Greets, Yes, a comment I have is that there is not enough context; you can't see what run_undetached returns and it's not clear as to whether or not CreateWindow() returned null. What are you trying to do that I may have missed from your code? Regards, Joe

        1 Reply Last reply
        0
        • S suninwater

          IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA

          S Offline
          S Offline
          Swinefeaster
          wrote on last edited by
          #4

          MFC does not support access of CWnd's from threads other than the ui thread, which I think is what you are doing. There are two approaches you can take: 1. Pass the HWND only to the other thread, and have it use the Windows api directly. 2. Use ::PostMessage() from the other thread on that window handle and trap the message in you main ui thread, and process it there. (I would strongly recommend this approach, as you really shouldn't be accessing windows from threads other than the main ui thread (except to post window messages)). Cheers, Swine [b]yte your digital photos with [ae]phid [p]hotokeeper - www.aephid.com.

          1 Reply Last reply
          0
          • D Dave Bryant

            The remarks in MSDN for IsWindow() actually say that a thread should not call IsWindow() for a window that it did not create because the window could be destroyed after the function is called - so that's not a promising start... Anyway, here's a couple of likely reasons for your problem: 1) Does the call to CreateWindow() succeed? If not, then obviously IsWindow() will not succeed either. 2) When does the window get destroyed? If it is destroyed in the MyClass destructor, then this will be called at the end of the scope of the MyClass object (since it is created on the stack). From the code snippet above, that means that it will be called when mainfunc() terminates, which, unless you have some sort of syncrhonisation, will be before your thread function terminates. Consequently, the window will be destroyed before the thread gets around to using it. Dave http://www.cloudsofheaven.org

            S Offline
            S Offline
            suninwater
            wrote on last edited by
            #5

            thanks for your reply ! :-) 1) CreateWindow succeeds clearly! 2) the window will be available till mainfunc() terminates. more help ?

            D 1 Reply Last reply
            0
            • S suninwater

              thanks for your reply ! :-) 1) CreateWindow succeeds clearly! 2) the window will be available till mainfunc() terminates. more help ?

              D Offline
              D Offline
              Dave Bryant
              wrote on last edited by
              #6

              Well that's your problem then. When mainfunc() terminates, your class will be destroyed, and so will the window. Then your other thread will be calling IsWindow() on a random piece of memory (the HWND has been destroyed, and so now is just a random number), so it will not work. You need to ensure that your class is not destroyed for the lifetime of the other thread. Dave http://www.cloudsofheaven.org

              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