IPC using RegisterWindowMessage

Posted on 2000-04-29
Last Modified: 2013-11-20
I have implemented IPC mechanism using RegisterWindowMessage .
The sending app allocates memory using GlobalAlloc and gets the pointer using GlobalLock and casts it to ptr to struct and assigns value to it. Then it calls GlobalUnlock and sends the HGLOBAL as lparam of PostMessage. The receiving app converts the HGLOBAL into
a pointer(GlobalLock) and the pointer is not NULL. But when I try to read the contents of the pointer I get an access violation. I tried various ways of GlobalLock etc but in vain.
Question by:mfc_speak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 32

Expert Comment

ID: 2761832
You CAN use RegisterWindowMessage to send messages between processes but you cannot send a memory pointer from one process to another.  Each process runs in its own virtual memory space and the addresses don't mix, at all.

There are a number of ways of doing this but the one that may lend itself to your method here is to use the WM_COPYDATA window message.

Author Comment

ID: 2761970
I feel that pointers can be shared across processes. I did a similar thing for clipboard copy and it worked fine. Since the global handles are allocated in the shared addr space common to all processes, I feel this should be possible.

Regarding WM_COPYDATA message, I read thru documentation. It says that an app must use SendMessage and not PostMessage to send it. But I need to send the data by PostMessage( which wont block my thread).
LVL 32

Accepted Solution

jhance earned 20 total points
ID: 2762074
I'm not really interested in HOW YOU FEEL about this.  


The clipboard is a different animal and is supported via the Windows API.  You don't operate on the clipboard via a pointer you get a handle to the clipboard from Windows.

The reason that WM_COPYDATA must use SendMessage is that the receiving application must accept the data while it is guaranteed to be valid.  If PostMessage were used the data might not still be available whenever the receiving process got around to accepting the message.

If you need some other scheme that doesn't rely on SendMessage you could use a memory mapped file, a disk file, a network socket connection, a pipe, a mailslot, or a shared memory area.

In none of the above cases, however, will you be sharing a memory pointer.  You will reference a handle to the object that Windows will manage for you.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2765361
WM_COPYDATA did not work with PostMessage. It worked only with SendMessage!!!
LVL 32

Expert Comment

ID: 2765488

Next time keep your stupid points.

Author Comment

ID: 2765787
Hey!!! dont get hot. I given you the grade you deserved (and points of course). You said that WM_COPYDATA works with PostMessage (but it didn't)
Any way :-)
LVL 32

Expert Comment

ID: 2767862
I said:

"The reason that WM_COPYDATA must use SendMessage..."

I did NOT say that WM_COPYDATA works with PostMessage!

I'm HOT because you graded me with a "D".  That's why you're a jerk!  If this answer was so damned bad you should have just rejected it.

Next time, keep your stinking points!!


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question