How to get string from SendMessage request (of a external exe) in c++

1.1k views Asked by At

I have two executables. I want to use one to get string from another one. In other words, I want to make a InterProcess Communication.
I tried SendMessage by using (char*)lParam in WndProc. However, it doesn't work. (char*)lParam makes a runtime error.
Here is part of my code:

the sender of SendMessage:

string s="12345";
SendMessage(hwnd,M_WR_SHELL,0,(LPARAM)s.c_str());


the receiver of SendMessage(in WndProc):

case M_WR_SHELL: {
    string s;
    s=(char*)lParam;
    MessageBox(NULL, s.c_str(),"THAT'S GOOD'",MB_OK);
    return 0;
}


I searched for the question. Some answers is like "They don't share the same memory space." However, I don't know how to deal with it.
If using SendMessage is not proper, how can I make it?

PS: I'm using TDM-GCC and don't want to use MFC and even VC++. If possible, please don't give me solutions through them. ;p

Thanks,
W. Xie

2

There are 2 answers

5
Kartik Maheshwari On BEST ANSWER

You need to use shared memory.I recommend using WM_COPYDATA instead of a user-defined message. WM_COPYDATA can only be sent via SendMessage() or SendMessageTimeout() and Windows will take care of copying the data into the receiving process address space.

3
Michael Chourdakis On

You have to use WM_COPYDATA (the easiest) or shared memory via file mapping functions. My own USM might help, but generally you need to invest more time in IPC mechanisms since you appear to come from another OS (hence the note about Visual Studio). IPC in Windows is not a simple task.