将消息发送到窗口句柄

本文关键字:窗口句柄 消息 | 更新日期: 2023-09-27 18:36:25

我尝试使用 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);
       }

将消息发送到窗口句柄

我稍微修改了源代码,它正在工作。从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;

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

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

也许一些基本的COM会帮助你。这很容易,您不再需要使用发送消息,窗口句柄等

COM 简介

COM 简介第 2 部分