Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SendMessage Not Passing

I want to pass object of below class to another application(C#)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public class xx
    {
        public int cursorPos { get; set; }
        public int selectionLength { get; set; }
    }

How I send;

private void btnSend_Click( object sender, EventArgs e )
        {          
            IntPtr handle = IntPtr.Zero;

            foreach ( Process proc in Process.GetProcesses() )
            {
                if ( proc.MainWindowTitle.StartsWith("RedBrowser.Net") )
                {
                    handle = proc.MainWindowHandle;
                }
            }

            if ( handle != IntPtr.Zero )
            {

                xx cs = new xx();
                cs.selectionLength = 10;

                Marshal.StructureToPtr(cs, Marshal.AllocCoTaskMem(Marshal.SizeOf(cs)), false);

                Win32.CopyDataStruct cds = new Win32.CopyDataStruct();
                cds.lpData = Marshal.AllocHGlobal(Marshal.SizeOf(cs));
                cds.cbData = Marshal.SizeOf(cs);    
                Marshal.StructureToPtr(cds, Marshal.AllocCoTaskMem(Marshal.SizeOf(cds)), false);
                Win32.SendMessage(handle, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);
            }            
            else
                MessageBox.Show("Not send");
        }

How I receive;

protected override void WndProc( ref Message m )
        {
            switch ( m.Msg )
            {
                case Win32.WM_COPYDATA:

                    Win32.CopyDataStruct cds = (Win32.CopyDataStruct)m.GetLParam(typeof(Win32.CopyDataStruct));

                    // If the size matches
                    if ( cds.cbData == Marshal.SizeOf(typeof(xx)) )
                    {
                        xx myStruct = (xx)Marshal.PtrToStructure(cds.lpData, typeof(xx));
                        MessageBox.Show(myStruct.selectionLength.ToString());
                    }
                    break;
                default:
                    // let the base class deal with it
                    base.WndProc(ref m);
                    break;
            }
        }

At first run I get some garbage value () and second, third,.. I get nothing (0)

What is wrong in here..?

like image 908
Buddhi Dananjaya Avatar asked Sep 14 '26 15:09

Buddhi Dananjaya


1 Answers

The problem is that whilst you allocate the pointer cds.lpData, you do not actually write anything to the block of memory that cds.lpData points to. So its contents are not initialised and could contain anything.

I think the structure would be much better declared as a simple structure like this:

public struct xx
{
    public int cursorPos;
    public int selectionLength;
}

I'd then use this code to send:

xx cs;
cs.cursorPos = 0;
cs.selectionLength = 10;
cds.cbData = Marshal.SizeOf(cs);    
cds.lpData = Marshal.AllocHGlobal(Marshal.SizeOf(cs));
try
{
    Marshal.StructureToPtr(cs, cds.lpData, false);
    Win32.SendMessage(handle, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);
}
finally
{
    Marshal.FreeHGlobal(cds.lpData);
}
like image 163
David Heffernan Avatar answered Sep 17 '26 04:09

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!