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);
}