WinForm实现跨进程通信的方法

public class WinMessageHelper
{
  private struct COPYDATASTRUCT
  {
    public IntPtr dwData;
    public int cbData;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpData;
  }
  //使用COPYDATA进行跨进程通信
  public const int WM_COPYDATA = 0x004A;
  [DllImport("User32.dll", EntryPoint = "SendMessage")]
  private static extern int SendMessage(
  int hWnd, // handle to destination window
  int Msg, // message
  int wParam, // first message parameter
  ref COPYDATASTRUCT lParam // second message parameter
  );
  [DllImport("User32.dll", EntryPoint = "FindWindow")]
  private static extern int FindWindow(string lpClassName, string lpWindowName);
  /// <summary>
  /// 发送消息
  /// </summary>
  /// <param name="windowReceiveTitle">接收方窗体标题名称</param>
  /// <param name="strData">要发送的数据</param>
  public static void Send(string windowReceiveTitle, string strData)
  {
    int winHandler = FindWindow(null,windowReceiveTitle);
    if (winHandler != 0)
    {
      byte[] sarr = System.Text.Encoding.Default.GetBytes(strData);
      int len = sarr.Length + 1;
      COPYDATASTRUCT cds;
      cds.dwData = (IntPtr)100;
      cds.lpData = strData;
      cds.cbData = len;
      SendMessage(winHandler, WM_COPYDATA, 0, ref cds);
    }
  }
  /// <summary>
  /// 接收消息
  /// </summary>
  /// <example>
  /// 在窗体中覆盖接收消息函数
  /// protected override void DefWndProc(ref System.Windows.Forms.Message m)
  /// {
  ///   switch(m.Msg)
  ///   {
  ///     case WinMessageHelper.WM_COPYDATA:
  ///       string str = WinMessageHelper.Receive(ref m);
  ///       break;
  ///     default:
  ///       base.DefWndProc(ref m);
  ///       break;
  ///
  ///   }
  /// }
  /// </example>
  /// <returns>接收的到数据</returns>
  public static string Receive(ref System.Windows.Forms.Message m)
  {
    COPYDATASTRUCT cds = new COPYDATASTRUCT();
    Type cdsType = cds.GetType();
    cds = (COPYDATASTRUCT)m.GetLParam(cdsType);
    return cds.lpData;
  }
}


知识共享许可协议
《WinForm实现跨进程通信的方法》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

Warning: file_get_contents(http://ip.taobao.com/service/getIpInfo.php?ip=3.226.122.74) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway in D:\wwwroot\changweihua\wwwroot\v\content\templates\darklight\vrecord.php on line 129

开发技术


开发平台和工具

sitemap     173.07ms