[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# sendmessage not working correctly, sending wrong keys?

Posted on 2009-02-22
1
Medium Priority
?
728 Views
Last Modified: 2013-12-14
What I am trying to do is loop through the chars of a textbox which contain a password, and then convert that char into the int version and send it to a third party program via sendmessage() to type in that password in that program. using spy++ it seems that it is pressing the number keys instead and the keydown and keyup are also out of order. I'm pretty sure the out of order is because it is being sent too fast so i can slow that down but not sure why the correct chars are not being sent. can anyone lend some advice?
private void enterPassword()
        {
         
            string password = "";
            int Handle = FindWindow(null, "programname");
            clickArea(Handle, 512, 714); //click on password field
            
            password = passwordTextBox.Text;
            passwordLetter = 0;
            int theLength = password.Length;
 
            for (int counter = 0; counter < password.Length; counter++)
                {
                convertLetter(password[counter].ToString());
                pressKey(Handle, passwordLetter, 0);
                passwordLetter = 0;
                }
 
            clickArea(Handle, 511, 729); //click connect
            Thread.Sleep(13000);
            clickArea(Handle,324, 276); //click on char
      
        }
 
 
 
 
 private void convertLetter(string letter)
        {
            
            
            switch (letter)
            {
                case "a":
                    passwordLetter = 0x61;
                    break;
                case "b":
                    passwordLetter = 0x62;
                    break;
                case "c":
                    passwordLetter = 0x63;
                    break;
                case "d":
                    passwordLetter = 0x64;
                    break;
                case "e":
                    passwordLetter = 0x65;
                    break;
                case "f":
                    passwordLetter = 0x66;
                    break;
                case "g":
                    passwordLetter = 0x67;
                    break;
                case "h":
                    passwordLetter = 0x68;
                    break;
                case "i":
                    passwordLetter = 0x69;
                    break;
                case "j":
                    passwordLetter = 0x6A;
                    break;
                case "k":
                    passwordLetter = 0x6B;
                    break;
                case "l":
                    passwordLetter = 0x6C;
                    break;
                case "m":
                    passwordLetter = 0x6D;
                    break;
                case "n":
                    passwordLetter = 0x6E;
                    break;
                case "o":
                    passwordLetter = 0x6F;
                    break;
                case "p":
                    passwordLetter = 0x70;
                    break;
                case "q":
                    passwordLetter = 0x71;
                    break;
                case "r":
                    passwordLetter = 0x72;
                    break;
                case "s":
                    passwordLetter = 0x73;
                    break;
                case "t":
                    passwordLetter = 0x74;
                    break;
                case "u":
                    passwordLetter = 0x75;
                    break;
                case "v":
                    passwordLetter = 0x76;
                    break;
                case "w":
                    passwordLetter = 0x77;
                    break;
                case "x":
                    passwordLetter = 0x78;
                    break;
                case "y":
                    passwordLetter = 0x79;
                    break;
                case "z":
                    passwordLetter = 0x7A;
                    break;
                case "A":
                    passwordLetter = 0x41;
                    break;
                case "B":
                    passwordLetter = 0x42;
                    break;
                case "C":
                    passwordLetter = 0x43;
                    break;
                case "D":
                    passwordLetter = 0x44;
                    break;
                case "E":
                    passwordLetter = 0x45;
                    break;
                case "F":
                    passwordLetter = 0x46;
                    break;
                case "G":
                    passwordLetter = 0x47;
                    break;
                case "H":
                    passwordLetter = 0x48;
                    break;
                case "I":
                    passwordLetter = 0x49;
                    break;
                case "J":
                    passwordLetter = 0x4A;
                    break;
                case "K":
                    passwordLetter = 0x4B;
                    break;
                case "L":
                    passwordLetter = 0x4C;
                    break;
                case "M":
                    passwordLetter = 0x4D;
                    break;
                case "N":
                    passwordLetter = 0x4E;
                    break;
                case "O":
                    passwordLetter = 0x4F;
                    break;
                case "P":
                    passwordLetter = 0x50;
                    break;
                case "Q":
                    passwordLetter = 0x51;
                    break;
                case "R":
                    passwordLetter = 0x52;
                    break;
                case "S":
                    passwordLetter = 0x53;
                    break;
                case "T":
                    passwordLetter = 0x54;
                    break;
                case "U":
                    passwordLetter = 0x55;
                    break;
                case "V":
                    passwordLetter = 0x56;
                    break;
                case "W":
                    passwordLetter = 0x57;
                    break;
                case "X":
                    passwordLetter = 0x58;
                    break;
                case "Y":
                    passwordLetter = 0x59;
                    break;
                case "Z":
                    passwordLetter = 0x5A;
                    break;
                case " ":
                    passwordLetter = 0x20;
                    break;
                case "1":
                    passwordLetter = 0x31;
                    break;
                case "2":
                    passwordLetter = 0x32;
                    break;
                case "3":
                    passwordLetter = 0x33;
                    break;
                case "4":
                    passwordLetter = 0x34;
                    break;
                case "5":
                    passwordLetter = 0x35;
                    break;
                case "6":
                    passwordLetter = 0x36;
                    break;
                case "7":
                    passwordLetter = 0x37;
                    break;
                case "8":
                    passwordLetter = 0x38;
                    break;
                case "9":
                    passwordLetter = 0x39;
                    break;
                case "0":
                    passwordLetter = 0x30;
                    break;
                case "`":
                    passwordLetter = 0x60;
                    break;
                case "-":
                    passwordLetter = 0x2D;
                    break;
                case "=":
                    passwordLetter = 0x3D;
                    break;
                case "[":
                    passwordLetter = 0x5B;
                    break;
                case "]":
                    passwordLetter = 0x5D;
                    break;
                case ";":
                    passwordLetter = 0x3B;
                    break;
                case "'":
                    passwordLetter = 0x27;
                    break;
                case ",":
                    passwordLetter = 0x2C;
                    break;
                case ".":
                    passwordLetter = 0x2E;
                    break;
                case "/":
                    passwordLetter = 0x2F;
                    break;
                case @"\":
                    passwordLetter = 0x5C;
                    break;
                case "~":
                    passwordLetter = 0x7E;
                    break;
                case "!":
                    passwordLetter = 0x21;
                    break;
                case "@":
                    passwordLetter = 0x40;
                    break;
                case "#":
                    passwordLetter = 0x23;
                    break;
                case "$":
                    passwordLetter = 0x24;
                    break;
                case "%":
                    passwordLetter = 0x25;
                    break;
                case "^":
                    passwordLetter = 0x5E;
                    break;
                case "&":
                    passwordLetter = 0x26;
                    break;
                case "*":
                    passwordLetter = 0x2A;
                    break;
                case "(":
                    passwordLetter = 0x28;
                    break;
                case ")":
                    passwordLetter = 0x29;
                    break;
                case "_":
                    passwordLetter = 0x5F;
                    break;
                case "+":
                    passwordLetter = 0x2B;
                    break;
                case "{":
                    passwordLetter = 0x7B;
                    break;
                case "}":
                    passwordLetter = 0x7D;
                    break;
                case ":":
                    passwordLetter = 0x3A;
                    break;
                case "\"":
                    passwordLetter = 0x22;
                    break;
                case "<":
                    passwordLetter = 0x3C;
                    break;
                case ">":
                    passwordLetter = 0x3E;
                    break;
                case "?":
                    passwordLetter = 0x3F;
                    break;
                case "|":
                    passwordLetter = 0x7C;
                    break;
            }//end switch statement
        }

Open in new window

0
Comment
Question by:willpv2
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 23706595
It looks lke you are "clicking" on an editable field and then sending it characters one by one...

...why not just send the entire string as a whole with WM_SETTEXT?  (also sent via SendMessage)
http://msdn.microsoft.com/en-us/library/ms632644(VS.85).aspx
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question