After a long search for the answer to this question, I realized that I was missing a VERY important step. I feel like an idiot, but here is the answer to my own question.
The C # structure should look like this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public unsafe struct DataStruct { public int x; public int y; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)] public string s; public double d; public char c; };
And another structure needs to be defined to receive WM_COPYDATA information. It looks like this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public unsafe struct CopyDataStruct { public IntPtr dwData; public int cbData; public IntPtr lpData; }
And the WndProc method should be changed so that it looks like this:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == 0x4A) { CopyDataStruct cps = (CopyDataStruct)Marshal.PtrToStructure(lParam, typeof(CopyDataStruct)); DataStruct data = (DataStruct)Marshal.PtrToStructure(cps.lpData, typeof(DataStruct)); updateText(data); handled = true; } return (IntPtr)0; }
I used CopyDataStruct in my previous working solution, and I just forgot to use it in a newer version.
source share