长话短说, 我试图在计算机启动时自动处理一些事情。 我认为我应该写一个 C# 控制台应用程序来做这个操作, 然后把它添加到启动时要执行的窗口中的时间表任务中。 我的问题在于一个程序, 它需要一个密码, 并且没有通过命令行打开的选项。 因此必须手工输入它。 我的想法是从 KeePass 数据库中检索我的密码, 并使用 SendKeys 输入密码和登录程序。 我的问题是要装入它的时间; 我无法在 GUI 接口装入时探测它, 并且已经准备好迎接我的 SendKeys 。 是否有办法检测这个程序? 我假设我只需要用“ Process” 类来操作这个程序。 还要注意的是, 当我使用程序运行执行程序时, 程序会创造另一个登录程序。 Start () 程序创建另一个程序, 但是它没有连接的窗口, 我可以用 Windows API 呼叫来查看 。
好吧,那是很长的, 我可以重温...
Problem: From C# detecting when a third party program has loaded (i.e. the splash screen is gone and GUI is ready for user interaction - meaning I can t just rely on if the Process is running or not). Also, no command line options for the third party program, or I would just run it with the password as an argument.
Goal: To use SendKeys in order to automate entering a password, but my program must wait for the third party application to finish loading.
Notes: Using C# .NET 3.5 Console Application NOT detecting load for my own form but a third party otherwise this would be easy (i.e. form_loaded event...)
谢谢你看我的问题 如果你想了解更多细节 或任何情况 通知我
<强度 > UPDATE 强度 > :
Problem solved! The two answers I received combined to give me the solution I wanted. So if anyone comes across this later, here is what I did to make it work.
因此这个程序将您必须登录的一些客户端软件的登录自动化。 我的问题是, 软件没有提供其它许多程序所提供的命令行写法的选项或文档, 这样您就可以登录一个密钥文件或其它东西。 这个程序也禁用了复制和粘贴, 密码会被手工输入, 如果您使用像我一样的密码, 很长的复杂密码而没有模式。 因此, 我为我和其他在工作的人写了这个程序; 我只是安排它在登录窗口机器时运行, 打开客户端软件, 并自动进行登录 。
//
// 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...
}