• Question

  • Hi,

    I need to send data from c++ to c# appication and came a cross this sample


    c++ side:

    		cds.cbData = sizeof(myStruct);
    		cds.lpData = (LPVOID)&myStruct;
    		// Send the COPYDATASTRUCT struct through the WM_COPYDATA message to
    		// the receiving Window.
    		SendMessage(hTargetWnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)&cds);

    where MyStruct is:

    struct MyStruct
    	int nNumber;
    	TCHAR szMessage[256];

    in the c# side

     case WindowNative.WM_COPYDATA:
                        // Get the COPYDATASTRUCT struct from LParam
                        COPYDATASTRUCT cds = (COPYDATASTRUCT)m.GetLParam(
                        if (cds.cbData == Marshal.SizeOf(typeof(MyStruct)))
                            // Marshal the data from the unmanaged memory block
                            object data = Marshal.PtrToStructure(cds.lpData, 
                            // Cast the data to MyStruct
                            MyStruct myStruct = (MyStruct)data;
                            // Display the MyStruct data
                            this.lbNumber.Text = myStruct.nNumber.ToString();
                            this.lbMessage.Text = myStruct.strMessage;


    public struct COPYDATASTRUCT
        public IntPtr dwData;       // Specifies data to be passed
        public int cbData;          // Specifies the data size in bytes
        public IntPtr lpData;       // Pointer to data to be passed

    and MyStruct is:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        internal struct MyStruct
            public int nNumber;
            /// <summary>
            /// TCHAR[256]
            /// </summary>
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string strMessage;

    My question is , what if i want to send from the C++ more complex struct , for example instead of a string i want to send any kind of structure (LPVOID )

    meaning i want to send from the c++ this structure

    struct MyStruct
    	int nNumber;
    	LPVOID data;

    Is it possible? what do i need to do on both c++ and c# side

    Please assist



    Wednesday, May 20, 2015 11:01 AM


  • Hi Coder,

    >>My question is , what if i want to send from the C++ more complex struct , for example instead of a string i want to send any kind of structure (LPVOID ) Is it possible?

    I am not familiar with  'LPVOID'  type.Per my understanding. It's possible. In C#,LPVOID and LPCVOID are defined as any type (variant). And they can accept any value. They can be marshaled as integers, strings, handles, or even compound types, anything you want. In addition, you can marshal them as System.IntPtr, so you can set them to the address of any object in memory. In addition, you can marshal them as pointers to object.

    Please refer to Marshaling with C# – Chapter 2: Marshaling Simple Types for more detailed information.

    Some sample about LPVOID, you can get some hints from following threads.

    converting IntPtr as c# struct pointer

    DLLImport -> how to handle a HANDLE in C#

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Fred Bao Thursday, May 28, 2015 6:04 AM
    • Edited by Kristin Xie Thursday, May 28, 2015 6:04 AM
    • Marked as answer by Equilibrium Monday, June 1, 2015 9:35 AM
    Thursday, May 21, 2015 9:24 AM