N
New Bee
I have a C++ app that is sending the following data to a C# app through WM_COPYDATA
message. The below is the custom structre that I am sending.
C++ code:
========
struct sData
{
private:
int id;
char szData[1024];
public:
sData()
{
memset(this, NULL, sizeof(sData));
}
public:
void SetData(int iApp, char* pszData)
{
id = iApp;
strcpy(szData, pszData);
}
};
sData data;
data.SetData(1, "Hello! from Native code.");
COPYDATASTRUCT ds;
ds.dwData = (WPARAM)0;
ds.cbData = sizeof(sData);
ds.lpData = &data;
r = SendMessage(hCWnd, WM_COPYDATA, (WPARAM)0, (LPARAM)&ds);
C# code:
=======
struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public IntPtr lpData;
}
struct Data
{
public int id;
public string buf;
}
In a class that implements MessageWindow.
protected override void WndProc(ref Message msg)
{
switch(msg.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT ds = (COPYDATASTRUCT)Marshal.PtrToStructure(msg.LParam,
typeof(COPYDATASTRUCT));
Data data = (Data)Marshal.PtrToStructure(ds.lpData, typeof(Data));
// <<--- Give me unsupported error
}
......
......
.....
message. The below is the custom structre that I am sending.
C++ code:
========
struct sData
{
private:
int id;
char szData[1024];
public:
sData()
{
memset(this, NULL, sizeof(sData));
}
public:
void SetData(int iApp, char* pszData)
{
id = iApp;
strcpy(szData, pszData);
}
};
sData data;
data.SetData(1, "Hello! from Native code.");
COPYDATASTRUCT ds;
ds.dwData = (WPARAM)0;
ds.cbData = sizeof(sData);
ds.lpData = &data;
r = SendMessage(hCWnd, WM_COPYDATA, (WPARAM)0, (LPARAM)&ds);
C# code:
=======
struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public IntPtr lpData;
}
struct Data
{
public int id;
public string buf;
}
In a class that implements MessageWindow.
protected override void WndProc(ref Message msg)
{
switch(msg.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT ds = (COPYDATASTRUCT)Marshal.PtrToStructure(msg.LParam,
typeof(COPYDATASTRUCT));
Data data = (Data)Marshal.PtrToStructure(ds.lpData, typeof(Data));
// <<--- Give me unsupported error
}
......
......
.....