The message Wpf message loop

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A few days to meet a problem need to check whether a WPF program has been running, if not running start passing parameters, if the operation will need to pass the message directly. Very simple transfer parameters in the absence of operation condition, we only need the Process CMD window to start and transfer parameters, processing in the program. But if the program has already started a little trouble, I have the experience of WinForm with time is Win32 api  SendMessage C# program, we need only DllImport can be called. After a search of WPF window and DispatcherObject Reflector, I spent most of the day finally found a System.Windows.Interop.HwndSource with AddHock methods can be added to the Win32 message mechanism to monitor. It is very easy and we just need the time to register MainWindow, listening to the Win32 message handling our 0x004A message.

The console code, the main application of the FindWindow in the Win32 method to find our form, SendMessage to send our message, there is no difference and WinForm, for the use of Win32 you can about the realization of the two application message communication problems in C# programming reference our life. Check Win32 Api rare copy, use borrowed.

The program:


  1. View code   
  2. using System;   
  3. using System.Collections.Generic;   
  4. using System.Linq;   
  5. using System.Text;   
  6. using System.Runtime.InteropServices;   
  7.  
  8. namespace ConsoleApplication1   
  9. {   
  10. class Program   

  11. {   

  12. static void Main(string[] args)   

  13. {   

  14. string ch = "";   

  15. while (ch.ToLower() != "q")   

  16. {   

  17.  
  18. if (!SendMessage("Window1", @"Hello,I am from Console Program:" + ch))   

  19. {   

  20. Console.WriteLine("no window");  

  21.  
  22. };   

  23. ch = Console.ReadLine();   

  24. }   

  25. }   

  26.  
  27. public static bool SendMessage(string windowName, string strMsg)   

  28. {   

  29. if (strMsg == nullreturn true;   

  30. IntPtr hwnd = (IntPtr)FindWindow(null, windowName

    );   

  31. if (hwnd != IntPtr.Zero)   

  32. {   

  33. CopyDataStruct cds;   

  34. cds.dwData = IntPtr.Zero;   

  35. cds.lpData = strMsg;   

  36. cds.cbData = System.Text.Encoding.Default.GetBytes(strMsg).Length + 1;   

  37. int fromWindowHandler = 0;   

  38. SendMessage(hwnd, 0x004A, fromWindowHandler, ref  cds);   

  39. return true;   

  40. }   

  41. return false;   

  42. }   

  43.  
  44. [DllImport("User32.dll", EntryPoint = "FindWindow")]   

  45. private static extern int FindWindow(string lpClassName, string lpWindowName);   

  46.  
  47. [DllImport("User32.dll", EntryPoint = "SendMessage")]   

  48.  
  49. private static extern int SendMessage   

  50.  
  51. (   

  52.  
  53. IntPtr hWnd,   

  54.  
  55. int Msg,   

  56.  
  57. int wParam,   

  58.  
  59. ref  CopyDataStruct lParam   

  60.  
  61. );   

  62.  
  63. }   

  64.  
  65. [StructLayout(LayoutKind.Sequential)]   

  66.  
  67. public struct CopyDataStruct   

  68. {   

  69.  
  70. public IntPtr dwData;   

  71.  
  72. public int cbData;   

  73.  
  74. [MarshalAs(UnmanagedType.LPStr)]   

  75.  
  76. public string lpData;   

  77.  
  78. }   

  79.  
  80. }  
  81. Copy code 

WPF application: mainly in the MainWindow loaded event subscription message listener: here AddHock System.Windows.Interop.HwndSource registration

The program:

  1. View code   
  2.  
  3. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)   
  4. {   

  5.  
  6. if (msg == 0x004A)   

  7. {   

  8.  
  9. CopyDataStruct cds = (CopyDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(CopyDataStruct));   

  10.  
  11. MessageBox.Show(cds.lpData);   

  12. this.Visibility = Visibility.Visible;   

  13. }   

  14.  
  15. return hwnd;   

  16.  
  17. }   

  18.  
  19. private void Window_Loaded(object sender, RoutedEventArgs e)   

  20. {

     

  21.  
  22. (PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource).AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));   

  23. }  

  24. Copy code 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Elton at November 15, 2013 - 3:34 AM