
How to send string with SendMessage?
I have two instances of my application running and the other sends a
message to the another:
IntPtr ip = Marshal.StringToHGlobalAnsi("blah blah blah");
SendMessage(hWnd, 0x1234, IntPtr.Zero, ip);
SendMessage is imported as:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd,
[MarshalAs(UnmanagedType.U4)] int Msg, IntPtr wParam, IntPtr lParam);
The another instance is trying to read the string this way:
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
switch (message.Msg) {
case 0x1234:
string str = Marshal.PtrToStringAnsi(message.LParam);
break;
}
Quote:
}
The resulting string is full of junk. What's the right way to
implement this?