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
  • 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.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 47
paragon account problem 9 96
repeatEnd java challenge 42 83
Line meaning 9 77
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now