title: How to use SendMessage to paste something to a different window
body: <p>I am using the following SendMessage function to send/paste text to a different application.
  But in that function I have to give the name of the window from the other application.</p>
  
  <p>How can I change this to get the current active window and paste in the code there?</p>
  
  <p>Code:</p>
  
  <pre><code>[DllImport("user32.dll")]  
  public static extern int SendMessage(int hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);  
  
  [DllImport("user32.dll", SetLastError = true)]  
  static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
  
  [DllImport("user32.dll", SetLastError = true)]  
  static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  
  public const int WM_PASTE = 0x0302;
  
  IntPtr windowHandle = FindWindow("NOTEPAD", null);  
  IntPtr editHandle = FindWindowEx(windowHandle, IntPtr.Zero, "EDIT", null);  
  string textToSendToFile = "Input here your text";
  Clipboard.SetText("Test");
  SendMessage((int)editHandle, WM_PASTE, 0, textToSendToFile); 
  </code></pre>
  
  <p>I also got this but I do not really know how to combine this with the code above...</p>
  
  <pre><code>[DllImportAttribute("user32.dll", EntryPoint = "GetForegroundWindow")]
  public static extern IntPtr GetForegroundWindow();
  
  [DllImportAttribute("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
  public static extern uint GetWindowThreadProcessId([InAttribute()] IntPtr hWnd, IntPtr lpdwProcessId);
  
  IntPtr hWndForegroundWindow = GetForegroundWindow();
  uint activeThreadID = GetWindowThreadProcessId(hWndForegroundWindow, IntPtr.Zero);
  </code></pre>