Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème de SendKeys dans un exe

    12 juin 2021 à 14:24:51

    Bonjour, je cherche à créer un fonctionnalité sur mon programme qui est : l'utilisateur renseigne un UserID et un Password dans 2 textbox différentes, lorsqu'il appuy sur le button3 mon appli lance un autre .exe et insert dans deux champs différents le contenu de UserID et Password. C'est une sorte d'external login.

    Le .exe appelé game.exe se lance bien, mais au moment d'écrire dans les champs il plante / relance en boucle le .exe et parfois écrit dans les champs : <b> Warning </b>

    Ceci est déclaré au début de mon programme

    #region KeyStateFlags
            [Flags()]
            public enum KeyStateFlags : int
            {
                Toggled = 0x0001,       //	 is toggled.
                AsyncDown = 0x0002,     //	 went down since last GetAsync call.
                PrevDown = 0x0040,      //	 was previously down.
                Down = 0x0080,      //	 is currently down.
                AnyCtrl = 0x40000000,   //  L or R control is down.
                AnyShift = 0x20000000,  //  L or R shift is down.
                AnyAlt = 0x10000000,    //  L or R alt is down.
                Capital = 0x08000000,   //  VK_CAPITAL is toggled.
                LeftCtrl = 0x04000000,  //  L control is down.
                LeftShift = 0x02000000, //  L shift is down.
                LeftAlt = 0x01000000,   //  L alt is down.
                LeftWin = 0x00800000,   //  L Win  is down.
                RightCtrl = 0x00400000, //  R control is down.
                RightShift = 0x00200000,    //  R shift is down.
                RightAlt = 0x00100000,  //  R alt is down.
                RightWin = 0x00080000,  //  R Win  is down.
                Dead = 0x00020000,  //  Corresponding char is dead char.
                NoCharacter = 0x00010000,   //  No corresponding char.
                Language1 = 0x00008000, //  Use for language specific shifts.
                NumLock = 0x00001000    //  NumLock toggled state.
            }
            #endregion
            [DllImport("user32.dll")]
            private static extern IntPtr GetForegroundWindow();
    
            [DllImport("user32.dll")]
            public static extern int SetForegroundWindow(IntPtr hWnd);
    
            [DllImport("user32.dll")]
            private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
    
            [DllImport("coredll.dll", EntryPoint = "PostKeybdMessage", SetLastError = true)]
            internal static extern bool PostKeybdMessage(IntPtr hwnd, uint vKey, KeyStateFlags flags, uint cCharacters, KeyStateFlags[] pShiftStateBuffer, uint[] pCharacterBuffer);
    
            const byte VK_RETURN = 0x0D;
            const uint KEYEVENTF_KEYUP = 0x0002;
            public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
            string user = "";
            string pass = "";



    Le contenu de mon button3.click

    private void button2_Click(object sender, EventArgs e)
            {
    
                label3.Text = "Starting th game...";
    
                Process.Start("game.exe", "start game");
    
                Process[] processes = Process.GetProcessesByName("game");
                Process proc = processes[0];
                label3.Text = "Now waiting the process to be active...";
    
                while (!proc.Responding)
                {
    
                    label3.Text = ("not responding");
                }
    
                label3.Text = "Process ok... waiting the game login";
                Thread.Sleep(19000);
                label3.Text = "Sending your ids....";
    
                string user = textBox1.Text;
                string pass = maskedTextBox1.Text;
    
                SetForegroundWindow(proc.MainWindowHandle);//foreground app
                Thread.Sleep(50);
                this.WindowState = FormWindowState.Minimized;
                Thread.Sleep(50);
                this.Show();
                this.WindowState = FormWindowState.Normal;
    
                Thread.Sleep(5000);
                SetForegroundWindow(proc.MainWindowHandle);//foreground app
    
                Thread.Sleep(100);
                System.Windows.Forms.SendKeys.SendWait(user);//User Input
                Thread.Sleep(100);
    
                keybd_event((byte)Keys.Tab, 0x8f, 0, 0); // Tab Press
                keybd_event((byte)Keys.Tab, 0x8f, KEYEVENTF_KEYUP, 0); // Tab Release
    
                Thread.Sleep(100);
                System.Windows.Forms.SendKeys.SendWait(pass); //Password input
    
    
    
                Thread.Sleep(100);
                SetForegroundWindow(proc.MainWindowHandle);//foreground app
    
                keybd_event(VK_RETURN, 0x9C, 0, 0);
                Thread.Sleep(100);
                System.Windows.Forms.Application.Exit();
            }
    • Partager sur Facebook
    • Partager sur Twitter

    Problème de SendKeys dans un exe

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown