Question

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..?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top