SendMessage to a window Handle

2019-07-18 01:40发布

问题:

I am try to use sendmessage to pass the message from my c++ application to c#

my c++ code is like this

int _tmain(int argc, _TCHAR* argv[])
{

 COPYDATASTRUCT cpd;
 cpd.dwData = 0;    
 LPCWSTR strDataToSend = L"http://google.com";;

     cpd.cbData = (wcslen(strDataToSend) + 1) * 2;
     cpd.lpData = (PVOID)strDataToSend; 
 SendMessage((HWND)0x0020073C,5555,0,(LPARAM)&cpd);


return 0;
}

i tried with SendMessageW also i hard coded the handle which is correct i am hetting the call back but not getting any data

my C# application is like this

    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }


    protected override void WndProc(ref Message m)
    {
        // Listen for operating system messages.

        switch (m.Msg)
        {
            // The WM_ACTIVATEAPP message occurs when the application
            // becomes the active application or becomes inactive.
            case 5555:

                COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                Type mytype = mystr.GetType();
                mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
                MessageBox.Show ( mystr.cbData.ToString());
                MessageBox.Show(mystr.lpData);
                break;
             }
             base.WndProc(ref m);
           }

i am getting the Messagebox blank...... i tried with out using COPYDATASTRUCT only my passing string plz can any one help me.. Thanks in Advance


  int _tmain(int argc, _TCHAR* argv[])
  {

  COPYDATASTRUCT cpd;
  cpd.dwData = 0;    
  LPCWSTR strDataToSend = L"http://google.com";;

   cpd.cbData = (wcslen(strDataToSend) + 1) * 2;
   cpd.lpData = (PVOID)strDataToSend; 
   SendMessage((HWND)0x0020073C,WM_COPYDATA,0,(LPARAM)&cpd);


 return 0;
}

C# code

public struct COPYDATASTRUCT
{
    public IntPtr dwData;
    public int cbData;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpData;
}
 public const int WM_COPYDATA = 0x4A;


protected override void WndProc(ref Message m)
{
    // Listen for operating system messages.

    switch (m.Msg)
    {
        // The WM_ACTIVATEAPP message occurs when the application
        // becomes the active application or becomes inactive.
        case WM_COPYDATA:

            COPYDATASTRUCT mystr = new COPYDATASTRUCT();
            Type mytype = mystr.GetType();
            mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
            MessageBox.Show ( mystr.cbData.ToString());
            MessageBox.Show(mystr.lpData);
            break;
         }
         base.WndProc(ref m);
       }

回答1:

I modified source code a bit and it's working. From C++ code I used this call

SendMessage((HWND)0x0020073C,WM_COPYDATA,0,(LPARAM)&cpd);

In C# application I used following

public const int WM_COPYDATA = 0x004A;

and COPYDATASTRUCT is

 [StructLayout(LayoutKind.Sequential)]
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            public IntPtr lpData;
        }

and message handler is modified like this

  case WM_COPYDATA:        

                    COPYDATASTRUCT mystr = new COPYDATASTRUCT();             

                    mystr = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));

                    if (mystr.cbData > 0)
                    {
                        byte[] data = new byte[mystr.cbData];
                        Marshal.Copy(mystr.lpData, data, 0, mystr.cbData);
                        Encoding unicodeStr = Encoding.Unicode;
                        char[] myString = unicodeStr.GetChars(data);
                        string returnText = new string(myString);
                        MessageBox.Show("Text is " + returnText);
                    }


                    break;


回答2:

What you are trying to do is some basic IPC (interprocess communication).

This is not a problem for C#, but for C++ it is.

Perhaps some basic COM will help you. It is quite easy and you no longer have to use SendMessage, Window handles and that like

Introduction to COM

Introduction to COM Part 2