How do I send a string to another window?
I have two applications and I need to send "text messages" between them. I tried PostMessage but I can only send numbers. I am using lParam to wrap the message, and if I change it to string
, I still only get numbers. Is it even possible to send a string, and if so, how?
Code below:
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_TEST = RegisterWindowMessage("WM_TEST");
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
Code for sending a message:
int message = 1234567890;
PostMessage((IntPtr)HWND_BROADCAST, WM_TEST, IntPtr.Zero, (IntPtr)message);
And get the message:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_TEST)
{
textBox1.AppendText(m.LParam.ToString() + Environment.NewLine);
}
base.WndProc(ref m);
}
So again the question is, how can I send string
between the two apps?
Thank!
source to share
It looks like an XY problem .
The best way to implement interprocess communication is to use network sockets , which can later be ported to allow remote interprocess communication. You are basically listening to a specific port in one application, and connecting to that port from another application, as if it were a remote host on the Internet.
Another way to implement inter-process communication is by using Anonymous Pipes , which is the Windows mechanism for IPC.
Typically, you can check out many different IPC methods here .
It is not recommended to use Windows Messages for this because that is not what they are for. WM are designed to allow the OS to notify a Windowed application of (mostly) UI interactions, even if any application can send WM to other applications. You have better options for that.
source to share