I am new to inter process communication and need some help. I want to be able to send a string from a C++ program to a C# program. My problem is that the resultant string is gibberish. Here is my code:
Sending program (C++):
void transmitState(char* myStr)
{
HWND hWnd = ::FindWindow(NULL, _T("myApp v.1.0"));
if (hWnd)
{
COPYDATASTRUCT cds;
::ZeroMemory(&cds, sizeof(COPYDATASTRUCT));
cds.dwData = 0;
cds.lpData = (PVOID) myStr;
cds.cbData = strlen(myStr) + 1;
::SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM)&cds);
}
}
And the receiving program (C#) (I have already overridden the WndProc):
private void OnCopyData(ref Message m)
{
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
String myStr;
unsafe
{
myStr = new String((char*) cds.lpData);
}
label1.Text = myStr;
}