WM_COPYDATA problem

Hi I would like to ask some help with following issue.

I need to send a string from a C# application to a Visual C++ application (win32) that uses SDL.
I tried to use a SendMessage WM_COPYDATA but without success, here some code to show you how I implemented it.
In the example, to simplify I tried to send a COPYDATASTRUCT filled with zeros and I expected to
get same values (zeros) into SDL.
Other custom messages (not WM_COPYDATA) are correctly managed between applications.-------------------------------------------------------------------------------------
C# code
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}

COPYDATASTRUCT cds;

public static IntPtr IntPtrAlloc(T param)
{
IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return (retval);
}
private void button1_Click(object sender, EventArgs e)
{
cds.dwData = (IntPtr)0;
cds.lpData = IntPtr.Zero;
cds.cbData = 0;
copyDataBuff = IntPtrAlloc(cds);
SendMessage(processMessageHandle, WM_COPYDATA, IntPtr.Zero, copyDataBuff);
}

Note:
I prepare a COPYDATASTRUCT filled with zeros and send a pointer as lparam to SDL application

C++/SDL code (message loop)

case SDL_SYSWMEVENT:
{
SDL_SysWMmsg *message = event.syswm.msg;
if (message->subsystem == SDL_SYSWM_WINDOWS)
{
HWND hwnd = message->msg.win.hwnd; /< The window for the message */
UINT msg = message->msg.win.msg; /
< The type of message /
WPARAM wParam = message->msg.win.wParam;/< WORD message parameter */
LPARAM lParam = message->msg.win.lParam;/
< LONG message parameter /
printf("%05i SDL_SYSWM_WINDOWS hwnd = %i msg = %i wParam = %i lParam = %i\n", eventCounter++, hwnd, msg, wParam, lParam);
if (msg == WM_COPYDATA)
{
COPYDATASTRUCT pcds = ((COPYDATASTRUCT)lParam);
char
a = (char
)&pcds;
for(i= 0; i < sizeof(COPYDATASTRUCT); i++)
printf("%i %i\n", i, a[i]);
}
}

Note: I receive the message, wparam is the same, lparam is completely different (is not clear for me if windows makes a copy of COPYDATASTRUCT
in the address space of SDL application) while for other messages wparam and lparam were exactly the same.
When I print COPYDATASTRUCT values are random (not Zeros)

You may need to handle this message within an event watcher, as Windows may
deallocate the message parameter after it has been processed:
https://wiki.libsdl.org/SDL_AddEventWatchOn Thu, Aug 14, 2014 at 1:54 AM, dbozza <bozza.davide at gmail.com> wrote:

Hi I would like to ask some help with following issue.

I need to send a string from a C# application to a Visual C++ application
(win32) that uses SDL.
I tried to use a SendMessage WM_COPYDATA but without success, here some
code to show you how I implemented it.
In the example, to simplify I tried to send a COPYDATASTRUCT filled with
zeros and I expected to
get same values (zeros) into SDL.
Other custom messages (not WM_COPYDATA) are correctly managed between
applications.


C# code
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}

COPYDATASTRUCT cds;

public static IntPtr IntPtrAlloc(T param)
{
IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return (retval);
}
private void button1_Click(object sender, EventArgs e)
{
cds.dwData = (IntPtr)0;
cds.lpData = IntPtr.Zero;
cds.cbData = 0;
copyDataBuff = IntPtrAlloc(cds);
SendMessage(processMessageHandle, WM_COPYDATA, IntPtr.Zero, copyDataBuff);
}

Note:
I prepare a COPYDATASTRUCT filled with zeros and send a pointer as lparam
to SDL application


C++/SDL code (message loop)

case SDL_SYSWMEVENT:
{
SDL_SysWMmsg *message = event.syswm.msg;
if (message->subsystem == SDL_SYSWM_WINDOWS)
{
HWND hwnd = message->msg.win.hwnd; /< The window for the message */
UINT msg = message->msg.win.msg; /
< The type of message /
WPARAM wParam = message->msg.win.wParam;/< WORD message parameter */
LPARAM lParam = message->msg.win.lParam;/
< LONG message parameter /
printf("%05i SDL_SYSWM_WINDOWS hwnd = %i msg = %i wParam = %i lParam =
%i\n", eventCounter++, hwnd, msg, wParam, lParam);
if (msg == WM_COPYDATA)
{
COPYDATASTRUCT pcds = ((COPYDATASTRUCT)lParam);
char
a = (char
)&pcds;
for(i= 0; i < sizeof(COPYDATASTRUCT); i++)
printf("%i %i\n", i, a[i]);
}
}

Note: I receive the message, wparam is the same, lparam is completely
different (is not clear for me if windows makes a copy of COPYDATASTRUCT
in the address space of SDL application) while for other messages wparam
and lparam were exactly the same.
When I print COPYDATASTRUCT values are random (not Zeros)


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you for your reply.
I tested it in my source code and it worked as you described.
Davide