Search code examples
c++stringwinapisendmessagelparam

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


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


Solution

  • 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.