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. Window Handle from CreateWindow will colse itself when thread exit?

Window Handle from CreateWindow will colse itself when thread exit?

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
5 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.
  • F Offline
    F Offline
    fitatc
    wrote on last edited by
    #1

    first, the MSDN about CreateWindow() not mention of close the handle of window it returns. :confused:is it means that we donnot need to care about it?(Sorry about the title if it is true) second, I test of this in the code like this

    HWND hWnd;
    unsigned __stdcall SecondThreadFunc( void* pArguments )
    {
    hWnd = CreateWindow(
    L"BUTTON"
    ,L"dmyWnd"
    ,WS_DISABLED
    ,CW_USEDEFAULT
    ,CW_USEDEFAULT
    ,0
    ,0
    ,NULL
    ,NULL
    ,::AfxGetInstanceHandle()
    ,NULL
    );
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    {
    if (bRet == -1)
    {
    // handle the error and possibly exit
    TRACE(L"[Thread] ERROR\n");
    }
    else
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    return 0;
    }

    _beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // called by MainThread

    I try to call

    int nRt = ::GetWindowText(hWnd, cs, 256);

    before and after I

    ::PostThreadMessage(threadID, WM_QUIT, 0, 0);

    to end the thread. The result is sccess when thread alive and invalid handle when thread exit. :confused:I am not sure if this can prove that window handle is be safely closed.Could any one tell me that is it true "Window Handle from CreateWindow will colse itself when thread exit"

    S S L 3 Replies Last reply
    0
    • F fitatc

      first, the MSDN about CreateWindow() not mention of close the handle of window it returns. :confused:is it means that we donnot need to care about it?(Sorry about the title if it is true) second, I test of this in the code like this

      HWND hWnd;
      unsigned __stdcall SecondThreadFunc( void* pArguments )
      {
      hWnd = CreateWindow(
      L"BUTTON"
      ,L"dmyWnd"
      ,WS_DISABLED
      ,CW_USEDEFAULT
      ,CW_USEDEFAULT
      ,0
      ,0
      ,NULL
      ,NULL
      ,::AfxGetInstanceHandle()
      ,NULL
      );
      while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
      {
      if (bRet == -1)
      {
      // handle the error and possibly exit
      TRACE(L"[Thread] ERROR\n");
      }
      else
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
      }
      return 0;
      }

      _beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // called by MainThread

      I try to call

      int nRt = ::GetWindowText(hWnd, cs, 256);

      before and after I

      ::PostThreadMessage(threadID, WM_QUIT, 0, 0);

      to end the thread. The result is sccess when thread alive and invalid handle when thread exit. :confused:I am not sure if this can prove that window handle is be safely closed.Could any one tell me that is it true "Window Handle from CreateWindow will colse itself when thread exit"

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      fitatc wrote:

      Could any one tell me that is it true "Window Handle from CreateWindow will colse itself when thread exit"

      No, not necessarily - it'll be because the thread's has gone. GetWindowText performs a SendMessage of WM_GETTEXT to the window you want. SendMessage to a window owned by another thread means switching to the thread and calling the window's WndProc. If the thread no longer exists, that's not possible, so the function returns an error.

      1 Reply Last reply
      0
      • F fitatc

        first, the MSDN about CreateWindow() not mention of close the handle of window it returns. :confused:is it means that we donnot need to care about it?(Sorry about the title if it is true) second, I test of this in the code like this

        HWND hWnd;
        unsigned __stdcall SecondThreadFunc( void* pArguments )
        {
        hWnd = CreateWindow(
        L"BUTTON"
        ,L"dmyWnd"
        ,WS_DISABLED
        ,CW_USEDEFAULT
        ,CW_USEDEFAULT
        ,0
        ,0
        ,NULL
        ,NULL
        ,::AfxGetInstanceHandle()
        ,NULL
        );
        while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
        {
        if (bRet == -1)
        {
        // handle the error and possibly exit
        TRACE(L"[Thread] ERROR\n");
        }
        else
        {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }
        }
        return 0;
        }

        _beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // called by MainThread

        I try to call

        int nRt = ::GetWindowText(hWnd, cs, 256);

        before and after I

        ::PostThreadMessage(threadID, WM_QUIT, 0, 0);

        to end the thread. The result is sccess when thread alive and invalid handle when thread exit. :confused:I am not sure if this can prove that window handle is be safely closed.Could any one tell me that is it true "Window Handle from CreateWindow will colse itself when thread exit"

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        To a certain extent windows (HWNDs) belong to the thread that created them. For example, the thread that created the window is the thread that pumps its messages. See Thread affinity of user interface objects, part 1: Window handles[^] for deatils. It doesn't make sense to create a window with a thread and then let it outlive its parent thread.

        Steve

        1 Reply Last reply
        0
        • F fitatc

          first, the MSDN about CreateWindow() not mention of close the handle of window it returns. :confused:is it means that we donnot need to care about it?(Sorry about the title if it is true) second, I test of this in the code like this

          HWND hWnd;
          unsigned __stdcall SecondThreadFunc( void* pArguments )
          {
          hWnd = CreateWindow(
          L"BUTTON"
          ,L"dmyWnd"
          ,WS_DISABLED
          ,CW_USEDEFAULT
          ,CW_USEDEFAULT
          ,0
          ,0
          ,NULL
          ,NULL
          ,::AfxGetInstanceHandle()
          ,NULL
          );
          while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
          {
          if (bRet == -1)
          {
          // handle the error and possibly exit
          TRACE(L"[Thread] ERROR\n");
          }
          else
          {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
          }
          }
          return 0;
          }

          _beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // called by MainThread

          I try to call

          int nRt = ::GetWindowText(hWnd, cs, 256);

          before and after I

          ::PostThreadMessage(threadID, WM_QUIT, 0, 0);

          to end the thread. The result is sccess when thread alive and invalid handle when thread exit. :confused:I am not sure if this can prove that window handle is be safely closed.Could any one tell me that is it true "Window Handle from CreateWindow will colse itself when thread exit"

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi,

          fitatc wrote:

          I am not sure if this can prove that window handle is be safely closed.Could any one tell me

          User applications do not manage window handles. To be more specific your application does not create/destroy or manage windows directly. The windows kernel does all of this for you when you request it. There is a device driver named 'Win32k.sys' which through ntdll.dll creates and manages the window for you. when you make a call to CreateWindow it ultimately passes through VerNtUserCreateWindowEx then ZwUserCreateWindowEx and your window is created. Event messages are then placed in the message que associated with the window. At this point your application can retrieve these messages and react to them. If you don't understand anything I just said... just remember that you do not need to close window handles. Best Wishes, -David Delaune

          F 1 Reply Last reply
          0
          • L Lost User

            Hi,

            fitatc wrote:

            I am not sure if this can prove that window handle is be safely closed.Could any one tell me

            User applications do not manage window handles. To be more specific your application does not create/destroy or manage windows directly. The windows kernel does all of this for you when you request it. There is a device driver named 'Win32k.sys' which through ntdll.dll creates and manages the window for you. when you make a call to CreateWindow it ultimately passes through VerNtUserCreateWindowEx then ZwUserCreateWindowEx and your window is created. Event messages are then placed in the message que associated with the window. At this point your application can retrieve these messages and react to them. If you don't understand anything I just said... just remember that you do not need to close window handles. Best Wishes, -David Delaune

            F Offline
            F Offline
            fitatc
            wrote on last edited by
            #5

            Thank you very much, that just the answer what I need!!! :-D :-D

            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