Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:38:10+00:00 2026-05-18T08:38:10+00:00

My console application spawns a new (invisible) window in its own thread. Prior to

  • 0

My console application spawns a new (invisible) window in its own thread. Prior to exiting the application, it attempts to clean up, and the last call to GetMessage in the window’s message pump fails. GetLastError returns 1400, “Invalid window handle.”

This is how the clean up proceeds in the application thread:

if ( s_hNotifyWindowThread != NULL )
{
    ASSERT(s_pobjNotifyWindow != NULL);

    ::PostMessage( s_pobjNotifyWindow->m_hWnd, WM_CLOSE, 0, 0 );
    ::WaitForSingleObject( s_hNotifyWindowThread, 50000L );      // Step 1: breakpoint here
    ::CloseHandle( s_hNotifyWindowThread );                      // Step 4: breakpoint here
    s_hNotifyWindowThread = NULL;
}

This WndProc exists in the new thread created for the window:

static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch ( uMsg )
    {
    case WM_CLOSE:
        ::DestroyWindow( hWnd );    // Step 2: breakpoint here
        break;

    case WM_DESTROY:
        ::PostQuitMessage( 0 );     // Step 3: breakpoint here
        break;

    case WM_DEVICECHANGE:
        /* Handle device change. */
        break;

    default:
        // Do nothing.
        break;
    }

    return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}

This is in the window’s thread function where the new window is created and my message pump is located:

s_pobjNotifyWindow = new CNotifyWindow( pParam );

while ( (bRetVal = ::GetMessage(
    &msg,                           // message structure
    s_pobjNotifyWindow->m_hWnd,     // handle to window whose messages are to be retrieved
    0,                              // lowest message value to retrieve
    0                               // highest message value to retrieve
    )) != 0 )
{
    switch ( bRetVal )
    {
    case -1:                        // Error generated in GetMessage.
        TRACE(_T("NotifyWindowThreadFn : Failed to get notify window message.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
        return ::GetLastError();    // Step 5: breakpoint here: Returns error 1400
        break;

    default:                        // Other message received.
        ::TranslateMessage( &msg );
        ::DispatchMessage( &msg );
        break;
    }
}

What am I doing wrong? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:38:11+00:00Added an answer on May 18, 2026 at 8:38 am

    You are passing an invalid window handle to GetMessage, which is why it’s failing and reporting that a window handle is invalid.

    You’ll see the same error if you run this code with a made-up window handle:

    MSG msg = {0};
    BOOL b = ::GetMessage(&msg, (HWND)(0x123000), 0, 0);
    
    if (b == -1)
    {
        DWORD dwErr = ::GetLastError();
        wprintf(L"%lu\n", dwErr);
    }
    

    The problem is that you are still using the window handle after the window has been destroyed. As soon as a window is destroyed its handle is invalid (or, worse, re-used by some other window). The message pump won’t exit until it processes the quit-message. Since the quit-message is posted during window destruction it will be processed after window destruction is complete. i.e. Your message pump keeps running for a short time after your window is destroyed.

    Just pass NULL to GetMessage for the window argument, so that it retrieves messages for all windows on that thread. Since the thread only exists for that one window it’s only going to get messages for that window anyway. (Plus the quit-message posted to the thread itself, and potentially messages for other windows which things like COM creates if you use COM on that thread… You’d definitely want to process those messages, so telling GetMessage to filter by window is doing nothing at best and could prevent something from working at worst, in addition to the error you’re seeing GetMessage return.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing console application in .Net and I want to change a behavior
In a .NET Win console application, I would like to access an App.config file
I have a console application that will be kicked off with a scheduler. If
I have a Console application hosting a WCF service. I would like to be
I have a trivial console application in .NET. It's just a test part of
I am writing a console application which makes use of the F1 key (for
I have a console application that require to use some code that need administrator
I have a console application project in C# 2.0 that needs to write something
I've got a console application that compiles and executes fine with Visual C++ 6.0,
This is a C# console application. I have a function that does something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.