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

  • Home
  • SEARCH
  • 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 6126341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:21:40+00:00 2026-05-23T16:21:40+00:00

Consider the following code: unsigned int __stdcall func( LPVOID ) { LRESULT result =

  • 0

Consider the following code:

unsigned int __stdcall func( LPVOID ) {
    LRESULT result = ::PostThreadMessage( ::GetCurrentThreadId(), 0, 0, 0 );
    return 0;
}

int wmain() {
    _beginthreadex( NULL, 0, func, NULL, 0, NULL );
    ...
}

Why does ::PostThreadMessage succeed? I think that it should fail because a message queue should not be created by that moment

  • 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-23T16:21:40+00:00Added an answer on May 23, 2026 at 4:21 pm

    Because you are calling PostThreadMessage() on the current thread, the system is able to create the message queue on demand. If you were calling PostThreadMessage() and passing the ID of a thread other than the calling thread, then it would fail if that thread did not have a message queue.

    For example, consider the following variant of your code:

    unsigned int __stdcall func( LPVOID ) {
        return 0;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        unsigned int threadID;
        _beginthreadex( NULL, 0, func, NULL, 0, &threadID );
        LRESULT result = ::PostThreadMessage( threadID, 0, 0, 0 );
        DWORD error = ::GetLastError();
        return 0;
    }
    

    Because we are now attempting to post the message from the main thread, to the worker thread, result comes back as 0 (i.e. an error), and error is set to ERROR_INVALID_THREAD_ID as described by the documentation for PostThreadMessage().

    If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError returns ERROR_INVALID_THREAD_ID if idThread is not a valid thread identifier, or if the thread specified by idThread does not have a message queue.

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

Sidebar

Related Questions

Consider the following non-working code: typedef map<int, unsigned> mymap; mymap m; for( int i
Consider the following code template<unsigned int N> void foo(std::bitset<N> bs) { /* whatever */
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code... double total = Int32.MaxValue; total++; int previousX = 0; for
Please, look at the following code that just convert an unsigned int to a
Consider following code public class City { public string Name { get { return
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code snippet below. class X { public String toString() { return

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.