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 6353061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:20:36+00:00 2026-05-24T22:20:36+00:00

I want to use windows’s message queue facilities to send a struct to another

  • 0

I want to use windows’s message queue facilities to send a struct to another thread.
But I find out that the postthreadmessage function only provide two integer parameters, lparam and wparam for me to pass arguments.So I decide to put the address of the struct in lparam. Is this the correct way windows use to pass struct?

And I intend to use boost::shared_ptr to hold the address of struct in both the receiver thread and sender thread. I doubt that when the two shared_ptrs goes out of scope, will the struct be freed twice? I can not figure out a way to ensure the struct allocated on heap will be 100% freed, Any ideas?

  • 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-24T22:20:38+00:00Added an answer on May 24, 2026 at 10:20 pm

    To the first question, yes, LPARAM is intended to be used as an integer or a pointer. That is clear from the definition:

    typedef LONG_PTR LPARAM;
    

    That is an integer long enough to hold a pointer.

    About the shared_ptr thing, you are right, if you pass the raw pointer and wrap it into another shared_ptr you will free it twice:

    shared_ptr<Thing> a;
    PostThreadMessage(x, 0, (LPARAM)a.get());
    ...
    LRESULT OnMessage(int msg, WPARAM wp, LPARAM lp)
    {
        shared_ptr<Thing> p((Thing*)lp); //Bad!!!
    }
    

    But you can try this workaround instead:

    shared_ptr<Thing> a;
    PostThreadMessage(x, 0, new shared_ptr<Thing>(a)); //pointer to smart-pointer
    ...
    LRESULT OnMessage(int msg, WPARAM wp, LPARAM lp)
    {
        shared_ptr<Thing> *pp = (shared_ptr<Thing>*)lp;
        shared_ptr<Thing> p(*pp);
        delete pp; //no leak
    }
    

    AFTERTHOUGHT: Note that PostThreadMessage may fail… and you don’t want to leak a shared_ptr.

    In my experience it is generally better to use a std::deque to hold the data and use the PostThreadMessage to notify that there is data there. In this way you’ll never lose an object! YMMV

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

Sidebar

Related Questions

I want to use ie7 on windows virutal pc, but to no avail. Below
I want to use windows apis in JRuby. Please suggest any api that can
I want to use windows 7 features like icon overlay, and thumbnail tasks but
I want to use WPF windows in a legacy win32 application. I'd like to
I'm new to emacs and I want to use ensime in Windows. I had
I want to use modal pop-up windows in our web app in Ruby on
I want to use delayed signing for my windows mobile compact framework assemblies. When
I want to use blob-leasing mechanism in my Windows Azure cloud app. I am
I want to use an AES 256-bit web service with a Windows Mobile 6.1
If you want to use a queuing product for durable messaging under Windows, running

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.