Feedback

Prozess starten und Tasten senden (SendKeys)

Sprache: C#

Ein Programm wird als Prozess gestartet, anschließend kann man Tasteneingaben an diesen Prozess senden
// Die Namensräume System.Runtime.InteropServices 
// und System.Diagnostics werden benötigt

[DllImport("User32.dll", SetLastError = true)]
public static extern int SetForegroundWindow(IntPtr hwnd);

int ID;
// Hier kann zum Beispiel "Notepad.exe" übergeben werden
private void ProzessErstellen(string Programmname)
{
    Process P = new Process();
    P.StartInfo.FileName = Programmname;
    P.Start();
    ID = P.Id;
}

private void TextSenden(string Text)
{
    System.IntPtr MainHandle = Process.GetProcessById(ID).MainWindowHandle;
    SendKeys.Send(Text);
    SetForegroundWindow(MainHandle);
}
// Die Namensräume System.Runtime.InteropServices 
// und System.Diagnostics werden benötigt

[DllImport("User32.dll", SetLastError = true)]
public static extern int SetForegroundWindow(IntPtr hwnd);

int ID;
// Hier kann zum Beispiel "Notepad.exe" übergeben werden
private void ProzessErstellen(string Programmname)
{
    Process P = new Process();
    P.StartInfo.FileName = Programmname;
    P.Start();
    ID = P.Id;
}

private void TextSenden(string Text)
{
    System.IntPtr MainHandle = Process.GetProcessById(ID).MainWindowHandle;
    SendKeys.Send(Text);
    SetForegroundWindow(MainHandle);
}

2 Kommentare

  1. In der Methode TextSenden müßte es nicht

    SetForegroundWindow(MainHandle);
    SendKeys.Send(Text);

    heißen?