簡単に言えば、コンピューターの起動時にいくつかのことを自動化しようとしています。これを行う C# コンソール アプリケーションを作成し、それを Windows のスケジュール タスクに追加して、起動時に実行することを考えました。私の問題は、1 つのプログラムにあり、パスワードが必要で、コマンド ラインから開くオプションがありません。したがって、手動で入力する必要があります。私が考えたのは、KeePass データベースからパスワードを取得し、SendKeys を使用してパスワードを入力し、プログラムにログインすることでした。私が抱えている問題は、ロードにかかる時間です。GUI インターフェースがいつロードされ、SendKeys の準備ができているかを検出する方法がありません。これを検出する方法はありますか?私が使用しなければならないのは「Process」クラスだけだと思います。これは、プログラムを実行するために使用したものだからです。また、Process.Start() を使用して実行可能ファイルを実行すると、
長くなりましたが、要約します...
問題: サードパーティ プログラムがロードされたことを検出する C# から (つまり、スプラッシュ スクリーンがなくなり、GUI がユーザー操作の準備ができている - つまり、プロセスが実行されているかどうかだけに頼ることはできません)。また、サードパーティ プログラムのコマンド ライン オプションがないか、パスワードを引数として実行するだけです。
目標: パスワードの入力を自動化するために SendKeys を使用しますが、プログラムはサードパーティ アプリケーションの読み込みが完了するまで待機する必要があります。
注: C# .NET 3.5 コンソール アプリケーションを使用すると、自分のフォームではなくサード パーティの負荷が検出されます。それ以外の場合は簡単です (つまり、form_loaded イベント...)
私の質問をご覧いただきありがとうございます。詳細や何か知りたいことがあれば教えてください。
更新:
問題が解決しました! 私が受け取った 2 つの回答を組み合わせて、私が望んでいた解決策を得ることができました。したがって、後で誰かがこれに出くわした場合、これを機能させるために私がしたことは次のとおりです。
そのため、このプログラムは、ログインが必要な一部のクライアント ソフトウェアへのログインを自動化します。私の問題は、キーファイルなどでログインできるように、他の多くのプログラムが提供するコマンドラインパラメーターのオプションやドキュメントをソフトウェアが提供しないことでした。このプログラムはコピーと貼り付けも無効にしているため、パスワードを手動で入力する必要があります。ですから、私はこのプログラムを自分のためだけでなく、職場の他の人のためにも書きました。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...
}