Click to See Complete Forum and Search --> : IPC getting the data out of a COPYDATASTRUCT


flavour404
April 21st, 2009, 07:35 PM
Hi,

I have written a c++ dll which sends my c# app a message - this is the dll:

COPYDATASTRUCT cds; //The structure used to send data.
char message[50];
strcpy(message, "This is the string passed via WM_COPYDATA.\0");

::ZeroMemory(&cds, sizeof(COPYDATASTRUCT));
cds.dwData = 0;
cds.cbData = strlen(message)+1));
cds.lpData = (PVOID)message;

::SendMessage(hWnd, WM_COPYDATA, (WPARAM)::GetDesktopWindow(), (LPARAM)&cds);

This seems to work, my question is how do I get the message string out of the struct on the c# end? I have created my struct and I know I have to do some 'marshalling' but at the moment all I am getting is a string full of empty characters:

COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
string strBufferData;
int unManagedSize = Marshal.SizeOf(typeof(COPYDATASTRUCT));
unsafe
{
byte* pString = (byte*)cds.lpData.ToPointer();
pString += unManagedSize;
IntPtr pManString = new IntPtr((void*)pString);
strBufferData = Marshal.PtrToStringUni(pManString);

// TODO: strBufferData contains the text sent over by the
// file monitor. Display it, analyze it, whatever.
}

//MessageBox.Show(strBufferData);

I would really like to know where I am going wrong...

Thanks.

vcdebugger
October 9th, 2009, 06:27 AM
instead of marshaling structure try to marshal the pointer to structure from the c++ side.
example :


IntPtr enumPointer;// Get this pointer filled from the dll side
ParserEnumsInfoTree enumsRootNode = (ParserEnumsInfoTree)
Marshal.PtrToStructure
(
enumPointer, //Pointer
typeof(ParserEnumsInfoTree)// Type
);

vcdebugger
October 9th, 2009, 06:44 AM
edited...