CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: postmessage

  1. #1
    Join Date
    Aug 2019
    Posts
    59

    postmessage

    Hi All,
    sorry for asking newbie questions. trying to learn the concept: Calling PostMessage from inside a thread function which could be member of a dialog class. When to omit the first parameter?. I see examples of both.
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    ::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
    also, how to use PostMessage with WaitForSingleObject , events or whatever else to get notified after the message in queue is processed.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,317

    Re: postmessage

    Quote Originally Posted by @EE@ View Post
    Hi All,
    sorry for asking newbie questions. trying to learn the concept: Calling PostMessage from inside a thread function which could be member of a dialog class. When to omit the first parameter?. I see examples of both.
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    ::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
    also, how to use PostMessage with WaitForSingleObject , events or whatever else to get notified after the message in queue is processed.
    This one:
    Code:
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    is correct presuming the dlghandle is valid HWND.

    Ths one:
    Code:
    ::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
    is wrong cause there is no such a function in a global scope ( :: means global scope).

    Also have a look at http://flounder.com/workerthreads.htm
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2019
    Posts
    59

    Re: postmessage

    what is difference between these two?:
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,730

    Re: postmessage

    This was answered by Victor above.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.4.4)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,317

    Re: postmessage

    Quote Originally Posted by @EE@ View Post
    what is difference between these two?:
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
    The first one is a Windows API function.
    The second one looks like a method of MFC CWnd class
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2019
    Posts
    59

    Re: postmessage

    victor and 2Kaud, thank you for very kind help. I learned a lot.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,230

    Re: postmessage

    Taking a look in your sample code...
    Code:
    ::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
    PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
    ...I have to add a little but important note, beside what Victor already answered. It looks like you are passing in WPARAM a pointer to an object (&s). This must be usually avoided, because unlike SendMessage function, PostMessage is asynchronous, i.e. it simply places the message in the message queue, then returns immediately. Until the message is processed, that object may be destroyed.

    Here is an example:
    Code:
    void CDemoDialog::OnSomeButtonClicked()
    {
        // ...
        CShape shape;
        PostMessage(WM_SOMEMSG, (WPARAM)&shape);
        //...
        // ...
    } // Here, 'shape' object goes out-of-scope and is being destroyed.
      // WM_SOMEMSG is not yet processed and further leads in troubles.
    In this case prefer using SendMessage.
    Last edited by ovidiucucu; October 27th, 2022 at 02:04 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpert.ro/blog/

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,317

    Re: postmessage

    Well I agree with Ovidiu about the danger in passing in WPARAM/LPARAM a pointer to an object (&s).
    However, there may be a following workaround (suggested some decades back by Joe Newcomer):
    1. create a new object in the heap
    2. post this address as WPARAM/LPARAM in the PostMessage
    3. the receiver (it is in the most case the main thread) is now responsible for proper deleting the received object
    Victor Nijegorodov

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,230

    Re: postmessage

    Thank you, Victor!
    Just to add a method in which the receiver doesn't need to delete: using of smart pointers, i.e. std::unique_ptr since C++11.
    Here is an example:

    Code:
    void CTestDlg::OnSomeButtonClicked()
    {
        auto spShape = std::make_unique<CShape>();
        // ...
        LPARAM lParam = reinterpret_cast<LPARAM>(spShape.get());
        PostMessage(WM_SOMEMSG, 0, lParam);
        spShape.release(); // release the pointer owmership
    }
    
    LRESULT CTestDlg::OnSomeMsg(WPARAM wParam, LPARAM lParam)
    {
        std::unique_ptr<CShape> spShape(reinterpret_cast<CShape*>(lParam));
        spShape->DoSomething();
        // ...
        // The receiver doesn't need to delete; 
        // it is automatically done when spShape goes out-of-scope.
        return 0;
    }
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpert.ro/blog/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured