Domanda

Così breve, corto, sto cercando di automatizzare alcune cose quando il mio computer si avvia. Pensavo che avrei scritto un'applicazione C # console per farlo e quindi aggiungerla a un'operazione di pianificazione in Windows da eseguire sull'avvio. Il mio problema è con un programma, richiede una password e non ha opzioni da aprire tramite la riga di comando. Quindi deve essere inserito manualmente. Il mio pensiero è stato quello di recuperare la mia password da un database KeepAss e utilizzare SendKeys per inserire la password e accedere al programma. Il problema che sto avendo è il momento necessario per caricare; Non ho modo di rilevare quando l'interfaccia GUI ha caricato ed è pronto per i miei sendini. C'è un modo per rilevare questo? Suppongo che tutto ciò che devo lavorare è la classe "Processo" da ciò che ho usato per gestire il programma. Si noti inoltre che quando eseguo l'eseguibile utilizzando il processo.start (), il programma crea un altro processo per l'accesso, ma non ha una finestra associata che posso vedere utilizzando le chiamate API di Windows.

Okay è stato lungo, posso ri-tatto ...

Problema: Da C # rilevando quando un programma di terze parti ha caricato (cioè lo schermo splash è sparito e la GUI è pronta per l'interazione dell'utente - il che significa che non posso fare affidamento solo se il processo è in esecuzione o meno). Inoltre, nessuna opzione di riga di comando per il programma di terze parti, o lo eseguirei solo con la password come argomento.

Obiettivo: Per utilizzare SendKeys al fine di automatizzare l'inserimento di una password, ma il mio programma deve attendere che l'applicazione di terze parti finisca il caricamento.

Note: Utilizzo dell'applicazione con console C # .NET 3.5 Non rilevare il carico per la mia forma ma una terza parte altrimenti questo sarebbe facile (cioè evento Form_loaded ...)

Grazie per aver esaminato la mia domanda, se vuoi più dettagli o qualsiasi cosa fammi sapere.

Aggiornamento :

problem solved! Le due risposte che ho ricevuto combinate per darmi la soluzione che volevo. Quindi se qualcuno ci arriva più tardi, ecco cosa ho fatto per farlo funzionare.

Quindi questo programma automatizza un login per alcuni software client da cui è necessario accedere. Il mio problema è stato che il software offerto non è possibile opzione o documentazione per i prametri della riga di comando che molti altri programmi offrono in modo da poter accedere con un keyfile o qualcosa del genere. Questo programma ha anche disabilitato copia e incolla in modo che la password debba essere digitata manualmente, il che è un grosso dolore se usi password come faccio, lunghi complicati senza motivi. Quindi ho scritto questo programma per il mio beneficio, anche gli altri al lavoro; Mi pianifica solo per eseguire l'accesso alla mia macchina Windows e apre il software client ed esegue l'accesso automaticamente.

//
// 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...
}
.

È stato utile?

Soluzione

È possibile verificare con il processo con processo.WaitForInputidle dopo aver avviato un processo e attendere fino a quando non è completamente avviata, ecco il semplice esempio:

http://msdn.microsoft.COM / IT-US / Biblioteca / XB73D10T% 28V= VS.71% 29.aspx

Altri suggerimenti

Prova a guardarti: http://www.acoolsip.com/a-cool-blog/Science-And-technology/151-C-Sendenti-Commands-Tependent-Windows.html

.

È possibile verificare se viene visualizzata la finestra (utilizzando il collegamento) e quindi inviare messaggi (anche sul collegamento)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top