PostMessage function

Posted on 2003-03-21
Medium Priority
Last Modified: 2013-11-20
I would like to do the following: in a program, send a notify message contain a CString to another window. So I have use the function PostMessage() and I succeed in notifying that window. However, I don't know how can I past a CString in WPARAM or LPARAM in the
PostMessage function parameter. Also, how can I retrieve the CString later in that Window?

here is my code:

#define WMAPP 0x8000

HWND hwnd = findWindow(0,"the window");
if (hwnd != null){
  // here i want to pass the CString in the WPARAM to send the message and later
  // in that window I can retrieve the CString

Question by:nkan
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
  • 2
  • 2

Accepted Solution

fl0yd earned 180 total points
ID: 8185934
This is not easily possible, since you are crossing process boundaries. If you pass the address of a CString object to another window and try to access data at that address chances are, that your application will crash, since the address is only valid in the calling process' address space. As far as I know you have two options:
* use the debug function WriteProcessMemory. You can find info about it here: http://msdn.microsoft.com/library/en-us/debug/base/debugging_functions.asp
* memory mapped files: Create a file mapping object specifying INVALID_HANDLE_VALUE as the hFile parameter to create a mapping object without creating a file associated with it. Info can be found here: http://msdn.microsoft.com/library/en-us/fileio/base/createfilemapping.asp
I would recommend using the latter approach. As a rather hackish alternative you could post a series of messages, each contain only one letter and reconstruct the string in the called window's message handler, using the terminating zero character to indicate the end of the string. Passing data across process boundaries that fits in one CPU register is possible. I'm not sure though, if it is guaranteed that the order of the posted messages will be the same that you used to post the messages, although I believe it is.

LVL 23

Expert Comment

by:Roshan Davis
ID: 8185936

Better you use SendMessage to any window while sending a string.
Because you have to allocate one char pointer and pass that pointer thru this API by casting it into WPARAM/LPARAM

The problem may occur when using PostMessage is that....The message you posting will delay in message queue.....and the memory tyou are allocated may be invalid because the sender function loses its Scope.

char *psz = new char[nHowMuch];

SendMessage(hwnd, WM_APP+2, (WPARAM)psz, NULL);
delete[] psz; // when you are Using PostMessage ...there may be chance of memory leak/ invalid memory

in receiving place

map the message , or catch in DefWindowProc


case WM_APP+2:
     char *psz = (char*) wParam;

Good Luck
LVL 23

Expert Comment

by:Roshan Davis
ID: 8185944
fl0yd is correct,

My comment is only work in windows that both in same process.

You can use WM_COPYDATA to pass data to another process.

MyCDS.dwData = MYPRINT;          // function identifier
   MyCDS.cbData = nStrLen;  // size of data
   MyCDS.lpData = psz;           // data structure

// Call function, passing data in &MyCDS

   hwDispatch = FindWindow( ..........);
   if( hwDispatch != NULL )
      SendMessage( hwDispatch,
                   (WPARAM)(HWND) hWnd,
                   (LPARAM) (LPVOID) &MyCDS );


Expert Comment

ID: 8185960
Now that is really funny. I had typed exactly the same as in your first post, when I suddenly realized that the data is sent to another process, just seconds before I pressed the 'submit' button :)


Expert Comment

ID: 8187245
floyd is right. memory mapped files is a standard alternative. if you post a series of messages with one char in wparam and the position of the char in lparam, could be reconstructed at recieving end without worrying abt order of messages.

Another way is to use shared data segments
 the following link tells about sharing data using the shared data segment in dlls.

the process which writes memory could load the dll, write in the shared variable and send a message to the destination window to load the dll and read from the shared variable to get data.

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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