3

I want to make a application which allows me to send keys or some messages to a not focused window.

My code which allows me to send messages to a windows when its focused:

string processName = "notepad";
Process[] processList = Process.GetProcesses();
foreach (Process P in processList)
{
    if (P.ProcessName.Equals(processName))
    {
        IntPtr edit = P.MainWindowHandle;
        SendKeys.Send("Hello");
    }
}

I hope I will find some help.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Sngr Zlyr
  • 31
  • 1
  • 2

1 Answers1

2

Heres a possible fix:

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
string processName = "notepad";

    Process [] notepads=Process.GetProcessesByName("notepad");
    if(notepads.Length==0)return;            
    if (notepads[0] != null)
    {
        IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessage(child, 0x000C, 0, "Hello");
    }
  • not work.Could not load type 'processbackground.Form1' from assembly 'processbackground, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'SendMessage' has no implementation (no RVA). – BREI May 18 '18 at 02:39