几天遇见一个问题需要检查某个wpf程序是否已经运行,如果没有运行则启动传递参数,如果已运行则需要直接传递消息。在没有运行 情况下传递参数很简单,我们只需要Process cmd窗口启动并传递参数,在程序中处理。但是如果程序已经启动有点麻烦,凭着我曾winform的经验第一时间想到的是win32 api  SendMessage,我们的C#程序只需要DllImport就可以调用了。经过一番查找和对wpf window和DispatcherObject的Reflector,花了我大半天终于找到了System.Windows.Interop.HwndSource中有AddHock方法可以添加对win32消息机制的监听。这下就很好办了我们只需要注册MainWindow的这个时间,来监听win32消息处理我们的0x004A消息。

控制台代码,主要应用的FindWindow 这个win32方法查找我们的窗体,SendMessage发送我们的消息,和winform没有什么差别,对于win32的使用你可以参考毒龙的程序人生 的关于C#中实现两个应用程序消息通讯的问题。难得查win32 Api直接copy,借来用用。

程序:

  1. 查看代码
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string ch = "";
  14. while (ch.ToLower() != "q")
  15. {
  16. if (!SendMessage("Window1", @"Hello,I am from Console Program:" + ch))
  17. {
  18. Console.WriteLine("no window");
  19. };
  20. ch = Console.ReadLine();
  21. }
  22. }
  23. public static bool SendMessage(string windowName, string strMsg)
  24. {
  25. if (strMsg == nullreturn true;
  26. IntPtr hwnd = (IntPtr)FindWindow(null, windowName   );
  27. if (hwnd != IntPtr.Zero)
  28. {
  29. CopyDataStruct cds;
  30. cds.dwData = IntPtr.Zero;
  31. cds.lpData = strMsg;
  32. cds.cbData = System.Text.Encoding.Default.GetBytes(strMsg).Length + 1;
  33. int fromWindowHandler = 0;
  34. SendMessage(hwnd, 0x004A, fromWindowHandler, ref  cds);
  35. return true;
  36. }
  37. return false;
  38. }
  39. [DllImport("User32.dll", EntryPoint = "FindWindow")]
  40. private static extern int FindWindow(string lpClassName, string lpWindowName);
  41. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  42. private static extern int SendMessage
  43. (
  44. IntPtr hWnd,
  45. int Msg,
  46. int wParam,
  47. ref  CopyDataStruct lParam
  48. );
  49. }
  50. [StructLayout(LayoutKind.Sequential)]
  51. public struct CopyDataStruct
  52. {
  53. public IntPtr dwData;
  54. public int cbData;
  55. [MarshalAs(UnmanagedType.LPStr)]
  56. public string lpData;
  57. }
  58. }
  59. 复制代码

wpf端程序:主要需要在MainWindow中loaded事件订阅消息监听:这里需要System.Windows.Interop.HwndSource的AddHock方法注册
程序:

  1. 查看代码
  2. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  3. {
  4. if (msg == 0x004A)
  5. {
  6. CopyDataStruct cds = (CopyDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(CopyDataStruct));
  7. MessageBox.Show(cds.lpData);
  8. this.Visibility = Visibility.Visible;
  9. }
  10. return hwnd;
  11. }
  12. private void Window_Loaded(object sender, RoutedEventArgs e)
  13. {
  14. (PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource).AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));
  15. }
  16. 复制代码

截个图:

很简单的东西结果被MS封装的不知哪里去,让我查了半天(其实应该是我的无知吧,不管怎么解决了就是心情舒畅了);