PostMessage function

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

Who is Participating?
fl0ydConnect With a Mentor Commented:
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:
* 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:
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.

Roshan DavisCommented:

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
Roshan DavisCommented:
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 );

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 :)

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.