-2

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

Asesh
  • 3,186
  • 2
  • 21
  • 31
Zhuoran Xie
  • 103
  • 2
  • 2
    Use WM_COPYDATA it's designed to do the job: https://msdn.microsoft.com/en-us/library/windows/desktop/ms649011(v=vs.85).aspx – Richard Critten Jun 02 '18 at 09:19
  • @RichardCritten : It seems that WM_COPYDATA doesn't support return value. However, I need to return something to the request sender. What's more, I need to send more than one argument to get the return value. – Zhuoran Xie Jun 02 '18 at 09:36
  • If you need to send a return do so by `WM_COPYDATA` in the other direction. `WM_COPYDATA` sends a block a data what you put in it is your "message". If you have requirements for multiple messages then invent a simple message format eg 1 byte - message ID; 1 byte - message length; message body. etc – Richard Critten Jun 02 '18 at 10:07
  • fyi: Inter-Process Communication (IPC) on MS-Windows from easy to hard: `WM_COPYDATA`; named pipes; TCP/IP; COM; RPC; shared-memory (although this is relative to your experience with the above mechanisms). – Richard Critten Jun 02 '18 at 10:13
  • @RichardCritten That seems too arcane and I may not be able to understand it in a short time :) Anyway, thanks. – Zhuoran Xie Jun 02 '18 at 11:03
  • 1
    Sometimes you need to learn new things – David Heffernan Jun 02 '18 at 12:42

2 Answers2

0

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.

  • See above: It seems that WM_COPYDATA doesn't support return value. However, I need to return something to the request sender. What's more, I need to send more than one argument to get the return value. – Zhuoran Xie Jun 02 '18 at 10:02
  • And maybe sometimes it will receive request from more than one process. – Zhuoran Xie Jun 02 '18 at 10:03
  • @谢卓然 you can use SendMessage with COPYDATASTRUCT, struct which consist your return data. You can't change SendMessage return type. And by WM_COPYDATA you will get return value in Boolean.In other hand I strongly recommend to use SendMessageTimeout(SMTO_ABORTIFHUNG) rather than SendMessage()! Otherwise if the receiving process is hanging the sending process will also hang eternally. – Kartik Maheshwari Jun 02 '18 at 10:09
  • Thanks a lot. I'll try it. – Zhuoran Xie Jun 02 '18 at 10:50
  • 1
    That's self-contradictory: If you insist, that using shared memory were strictly required, your conclusion to use a scheme that is based on copying data cannot work. – IInspectable Jun 02 '18 at 12:33
0

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.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • Well, I think the file mapping function may not be simple. And see above: It seems that WM_COPYDATA doesn't support return value. However, I need to return something to the request sender. What's more, I need to send more than one argument to get the return value. – Zhuoran Xie Jun 02 '18 at 10:04
  • Maybe sometimes it will receive request from more than one process. – Zhuoran Xie Jun 02 '18 at 10:04
  • Just a note that you seem to come from linux where processes communicate with each other more frequently, in Windows, this is not the case. – Michael Chourdakis Jun 02 '18 at 10:09