none
IPC via WM_COPYDATA RRS feed

  • Question

  • Hi,

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

    CppSendWM_COPYDATA && CSReceiveWM_COPYDATA.

    c++ side:

    COPYDATASTRUCT cds;
    		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(
                            typeof(COPYDATASTRUCT));
    
                        if (cds.cbData == Marshal.SizeOf(typeof(MyStruct)))
                        {
                            // Marshal the data from the unmanaged memory block
                            object data = Marshal.PtrToStructure(cds.lpData, 
                                typeof(MyStruct));
                            
                            // Cast the data to MyStruct
                            MyStruct myStruct = (MyStruct)data;
    
                            // Display the MyStruct data
                            this.lbNumber.Text = myStruct.nNumber.ToString();
                            this.lbMessage.Text = myStruct.strMessage;
                        }
                        break;
                }


    where COPYDATASTRUCT is

    StructLayout(LayoutKind.Sequential)]
    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

    Y.


    Coder

    Wednesday, May 20, 2015 11:01 AM

Answers

  • 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,

    Kristin


    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.





    Thursday, May 21, 2015 9:24 AM