SendMessage到窗口句柄

时间:2012-06-06 06:52:07

标签: c# c++ sendmessage

我尝试使用sendmessage将消息从我的c ++应用程序传递给c#

我的c ++代码就像这样

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;
}

我尝试使用SendMessageW我也硬编码了句柄,这是正确的我正在回复但没有得到任何数据

我的C#应用​​程序就像这样

    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);
           }

我收到消息框空白...... 我尝试没有使用COPYDATASTRUCT只有我传递的字符串plz可以任何人帮助我.. 在此先感谢


  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#代码

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);
       }

2 个答案:

答案 0 :(得分:0)

我修改了一些源代码并且它正在工作。从C ++代码我使用了这个调用

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

在C#应用程序中,我使用了以下

public const int WM_COPYDATA = 0x004A;

COPYDATASTRUCT

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

和消息处理程序像这样修改

  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;

答案 1 :(得分:0)

您要做的是一些基本的IPC(进程间通信)。

这对C#来说不是问题,但对于C ++来说却是。

也许一些基本的COM会帮助你。它非常简单,您不再需要使用SendMessage,Window句柄等

Introduction to COM

Introduction to COM Part 2