处理外部应用程序并填充输入字段

时间:2018-04-13 22:44:56

标签: c# wpf pinvoke findwindow

我有一个C#Windows桌面应用程序需要处理给定的外部应用程序, - 非托管代码 - 并填充它的输入字段。

请注意,这必须以编程方式完成,因此使用SPY ++来获取Windows名称是不可讨论的,我只能使用SPY ++获取Windows类。

在那里查看和阅读我发现它可以通过以下步骤完成(pseducode):

  1. 获取或启动外部应用程序流程。
  2. 使用FindWindow();
  3. 获取主窗口
  4. 使用FindWindowEx获取childWindows,它们是输入字段。
  5. 使用Sendmessage()发送消息。
  6. 我已经测试了填充记事本,但是处理我给定的应用程序一直是PITA。无法填充任何内容。

    这是我的实际代码:

      /*
      List of references, which includes System.Diagnostics, 
      System.Runtime.InteropServices,...
     */
      [DllImport("user32.dll")]
      public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
      [DllImport("user32.dll")]
      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);
    
    
      private void click_test(object sender, RoutedEventArgs e)
        {
           Process[] apps = Process.GetProcessesByName("givenApp");
    
           if (apps.Length == 0) return;
           if (apps[0] != null)
           {
    
                IntPtr mainWindow = FindWindow("class name", null);
                IntPtr child = FindWindowEx(apps[0].MainWindowHandle, new IntPtr(0), "Class Name", null);
                SendMessage(child, 0x000C, 0, input_test.Text);
            }
        }
    

    使用此代码我可以填充记事本。然而,填充记事本是一个测试阶段。我需要填充其他应用程序。

    缺少什么,出了什么问题?

1 个答案:

答案 0 :(得分:0)

我发现错误是什么,只是:

实例化子节点时,不要调用泛型MainWindowHadle,而是调用App Window句柄,该句柄先前在名为MainWindow的变量中定义,该变量调用FindWindow()类。

这是错误的:

IntPtr child = FindWindowEx(mainWindow, new IntPtr(0), "Class Name", null);

这是正确的:

{{1}}