How to send string with SendMessage? 
Author Message
 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?


Fri, 15 Apr 2005 01:53:44 GMT  
 How to send string with SendMessage?
Hi,

The pointer you are passing is not valid in the target process. In the
days of 16bit windows the HGlobal was OS global but no longer is
this the case.

You could use the standard message WM_COPYDATA,
this message takes a struct as the LPARAM and the data is
serialized with the message to the other process.

The other way is to use some form of interprocess communication
such as Memory Mapped file or Pipes. If I am not mistaken the
WM_COPYDATA goes the Memory Mapped file route.

Cheers

Chris


Quote:
> 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;
>    }
> }

> The resulting string is full of junk. What's the right way to
> implement this?



Fri, 15 Apr 2005 04:24:26 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to send the API SendMessage with the CHARFORMAT2 structure - wave undereline like word

2. What is the correct way of using SendMessage to send CTRL-A

3. Intercepting a message sent by SendMessage()

4. *** Sending a message (SendMessage?) ***

5. CAN'T SEND A MESSAGE TO a DIALOG CONTROL with SENDMESSAGE

6. Sending msg with 'SendMessage'?

7. intercept a message sent with SendMessage() ??

8. Sending Text using SendMessage

9. Crashing when sending a pointer to a large chunk of shared memory via an IPC SendMessage

10. ::SendMessage want to send more than one character

11. How to Using SendMessage to sent a VK_SHIFT??

12. intercept a message sent with SendMessage() ??

 

 
Powered by phpBB® Forum Software