【问题标题】:C# SendKeys Wait for Program to Load before sendingC# SendKeys 在发送前等待程序加载
【发布时间】:2012-05-22 21:03:26
【问题描述】:

长话短说,我正在尝试在我的计算机启动时自动执行一些操作。我想我会编写一个 C# 控制台应用程序来执行此操作,然后将其添加到 Windows 中的计划任务中,以便在启动时执行。我的问题是一个程序,它需要密码并且没有通过命令行打开的选项。因此必须手动输入。我的想法是从 KeePass 数据库中检索我的密码并使用 SendKeys 输入密码并登录到程序。我遇到的问题是加载时间;我无法检测 GUI 界面何时已加载并准备好使用我的 SendKeys。有没有办法检测到这一点?我假设我必须使用的只是“进程”类,因为那是我用来运行程序的。另请注意,当我使用 Process.Start() 运行可执行文件时,程序会创建另一个登录进程,但它没有我可以使用 Windows API 调用看到的关联窗口。

好吧,那篇幅太长了,我可以再说一遍……

问题: 从 C# 检测第三方程序何时加载(即启动画面消失并且 GUI 已准备好进行用户交互 - 这意味着我不能仅仅依赖进程是否正在运行)。 此外,第三方程序没有命令行选项,或者我只是使用密码作为参数运行它。

目标: 使用 SendKeys 来自动输入密码,但我的程序必须等待第三方应用程序完成加载。

注意事项: 使用 C# .NET 3.5 控制台应用程序 不检测我自己的表单但第三方的负载,否则这很容易(即 form_loaded 事件......)

感谢您查看我的问题,如果您想了解更多详细信息或任何事情,请告诉我。

更新

问题解决了! 我收到的两个答案相结合,为我提供了我想要的解决方案。因此,如果以后有人遇到这个问题,我就是这样做的。

因此,该程序会自动登录您必须登录的某些客户端软件。我的问题是该软件没有提供许多其他程序提供的命令行参数选项或文档,因此您可以使用密钥文件或其他东西登录。该程序还禁用了复制和粘贴功能,因此必须手动输入密码,如果您像我一样使用密码,这是一个很大的痛苦,长而复杂的密码没有模式。所以我写这个程序是为了我和工作中的其他人的利益;我只是安排它在登录到我的 windows 机器时运行,它会打开客户端软件并自动执行登录。

//
// IMPORTANT Windows API imports....
//

[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint procId);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 

[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);


// When I get to this point in my code, I already had the password and window title...
string password = "password";
string title = "window title";

// This gets a handle to the window I want where "title" is the text in the title
// bar of the window as a string.
// This is a Windows API function that must be imported by DLLImport
// I had to get the handle this way because all I knew about the third party
// window was the title, not the process name or anything...
IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, title);

// Now that I have a handle to the login window I used another windows API
// call to get the process ID.

// This windows API call gives me the Process ID as an out parameter and returns
// the thread ID of the window. I don't use the thread it, but maybe you can...
uint loginWindowProcId;
uint loginWindowThreadId = GetWindowThreadProcessId(hWnd, out loginWindowProcId);

// now I can just use .NET to find the Process for me...
Process loginWindowProcess = null;

if (0 != loginWindowProcId)
{
  // get the process object
  loginWindowProcess = Process.GetProcessById((int)loginWindowProcId);

  // This right here is why I wanted the Process structure. It takes a
  // little while for the client software to load and be ready. So here
  // you wait for the window to be idle so you know it has loaded and can
  // receive user input, or in this case keys from "SendKeys".
  loginWindowProcess.WaitForInputIdle();

  // I use yet another windows API call to make sure that the login window
  // is currently in the foreground. This ensures that the keys are sent
  // to the right window. Use the handle that we started with.
  SetForegroundWindow(hWnd);

  // Now send the password to the window. In my case, the user name is 
  // always there from my windows credentials. So normally I would type in the
  // password and press ENTER to login. But here I'll use SendKeys to mimic my
  // behavior.
  SendKeys.SendWait(password);   // send password string
  SendKeys.SendWait("{ENTER}");  // send ENTER key

  // Now the client should be logging in for you! : )

  // IMPORTANT NOTE
  // If you are using a console application like I am, you must add a reference to
  // System.Windows.Forms to your project and put "using System.Windows.Forms;" in
  // your code. This is required to use the "SendKeys" function.
  //
  // Also this code is just for my testing (quick and dirty), you will want to write 
  // more checks and catch errors and such. You should probably give the 
  // WaitForInputIdle a timeout etc...
}

【问题讨论】:

    标签: c# process executable sendkeys loaded


    【解决方案1】:

    您可以在启动进程后检查 Process.WaitForInputIdle,并等待完全启动,这是一个简单的示例:

    http://msdn.microsoft.com/en-us/library/xb73d10t%28v=vs.71%29.aspx

    【讨论】:

    • 感谢您的建议,我收到了另一个项目,但我现在又回到了这个项目。明天我会试一试,看看它是否可以与我所拥有的一起使用。
    【解决方案2】:

    试试看:http://www.acoolsip.com/a-cool-blog/science-and-technology/151-c-sending-commands-to-independent-windows.html

    您可以检查窗口是否显示(使用链接)然后发送消息(也在链接上)

    【讨论】:

    • 谢谢,我会试一试。这与我正在采取的方法略有不同。也许这是我需要的!
    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 2021-02-05
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多