2

我尝试使用 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 只有我传递的字符串请任何人帮助我.. 提前致谢


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

2 回答 2

0

您正在尝试做的是一些基本的 IPC(进程间通信)。

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

也许一些基本的 COM 会帮助你。这很简单,您不再需要使用 SendMessage、窗口句柄等

COM简介

COM 简介第 2 部分

于 2012-06-11T07:58:41.667 回答
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;
于 2012-06-11T07:48:56.493 回答