Question

Tellement longue histoire courte, j'essaie d'automatiser certaines choses lorsque mon ordinateur démarre. Je pensais écrire une application C # Console pour le faire, puis l'ajouter à une tâche de planification sous Windows à exécuter sur le démarrage. Mon problème est avec un programme, il nécessite un mot de passe et n'a aucune option à ouvrir via la ligne de commande. Ainsi, il doit être entré manuellement. Ma pensée était de récupérer mon mot de passe à partir d'une base de données Keepass et d'utiliser SendKeys pour entrer le mot de passe et vous connecter au programme. Le problème que je vais avoir est le temps qu'il faut pour charger; Je n'ai aucun moyen de détecter lorsque l'interface GUI est chargée et est prête pour mes envois SendKeys. Y a-t-il un moyen de détecter cela? Je suppose que tout ce que je dois travailler est le cours "processus", car c'est ce que j'avais l'habitude d'exécuter le programme. Notez également que lorsque j'exécute l'exécutable à l'aide de processus.Start (), le programme crée un autre processus de journalisation, mais il n'a aucune fenêtre associée que je peux voir à l'aide d'appels d'API Windows.

Ok ce fut long, je peux refaire ...

problème: De C # Détection lorsqu'un programme tiers a chargé (c'est-à-dire que l'écran SPLASH est parti et GUI est prêt pour l'interaction utilisateur - ce qui signifie que je ne peux pas compter si le processus est en cours d'exécution ou non). En outre, aucune option de ligne de commande pour le programme tiers, ou je ne l'exécuterais pas avec le mot de passe comme argument.

objectif: Pour utiliser SendKeys afin d'automatiser la saisie d'un mot de passe, mais mon programme doit attendre que l'application tierce partie de terminer le chargement.

Notes: Utilisation de l'application C # .NET 3.5 Console Ne pas détecter la charge pour ma propre forme, mais une tierce partie sinon ce serait facile (c'est-à-dire l'événement Form_Loaded ...)

Merci d'avoir examiné ma question, si vous voulez plus de détails ou quelque chose, faites le moi savoir.

mise à jour :

problème résolu! Les deux réponses que j'ai reçues combinées pour me donner la solution que je voulais. Donc, si quelqu'un y passe plus tard, voici ce que j'ai fait pour le faire fonctionner.

Ce programme automatise un logiciel de connexion pour certains logiciels client que vous devez vous connecter. Mon problème était que le logiciel n'offrait pas d'option ni de documentation pour les pramètres de ligne de commande que de nombreux autres programmes proposent afin que vous puissiez vous connecter avec un fichier clé ou quelque chose. Ce programme désactivait également la copie et la coller pour que le mot de passe doit être saisi manuellement, ce qui est une grosse douleur si vous utilisez des mots de passe comme je le fais, de longs complexes sans motifs. J'ai donc écrit ce programme pour mon avantage, ainsi que d'autres au travail; Je viens de le programmer pour exécuter à la connexion à ma machine Windows et ouvre le logiciel client et effectue automatiquement la connexion.

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

Était-ce utile?

La solution

Vous pouvez vérifier avec processus.WaitForinPutiveDle après avoir démarré un processus et attendre que ce soit complètement démarré, voici l'exemple simple:

http://msdn.microsoft.COM / EN-US / Bibliothèque / XB73D10T% 28V= vs.71% 29.aspx

Autres conseils

Essayez de regarder cela: http://www.acoolsip.com/a-cool-blog/science-and-technology/151-c-sending-commands-a-independen-windows.html

Vous pouvez vérifier si la fenêtre est affichée (à l'aide du lien), puis envoyez des messages (également sur le lien)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top