We are trying to automate an input process on a web based application. We are able to do the login and then by using a Java script (the tool bar is in java) select the appropriate section for input. What happens is another window appears and we have no access to it. we can't use the URL because each page / window is verified so llnking to it is not feasable at least as far as I know. Does anyone know how to identify the window and set focus to it so that we can automate the entry to it?

We are trying to automate an input process on a web based application. We are able to do the login and then by using a Java script (the tool bar is in java) select the appropriate section for input. What happens is another window appears and we have no access to it. we can't use the URL because each page / window is verified so llnking to it is not feasable at least as far as I know. Does anyone know how to identify the window and set focus to it so that we can automate the entry to it?

I wrote a program at work to automatically enter a license key into another program to avoid having to type it out all the time... It's called MobileXXX but has nothing to do with porn haha. (Use Spy++ to figuire out the window/control you are looking for.) I'll just post the code here:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;

namespace MacroThinger
{
    public partial class Form1 : Form
    {
        InjectorVersion iV = InjectorVersion.Tech;            //Set the current type of injector - redist is self destructive and non-copyable tech is just a macro
        DateTime MAX_DATE = new DateTime(2011, 4, 6,12,4,0);    //Used with redist type - current date
        const string SEND_TEXT = "somedatagoeshere";       //license key to inject into MobileXXX
    #region Dont change
        enum InjectorVersion { Redist, Tech };
        #region Native Win32 DLL Interops
        // Get a handle to an application window.
        const int BUTTON_CLICK = 0xF5;
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);
        [DllImport("user32.dll")]
            static extern int SendMessage(
                  IntPtr hWnd,      // handle to destination window
                  int Msg,          // message
                  IntPtr wParam,    // first message parameter
                  IntPtr lParam     // second message parameter
                  );
        [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool FreeConsole();

        #endregion
        public Form1()
        {
            InitializeComponent();
        }
        unsafe private void Form1_Load(object sender, EventArgs e)
        {
            //Check if the creation time is greater than the preset time - this would indicate the file has been copied
            //or it is in use past its expiry date
            //If it is a tech build, it will close the program
            if (System.IO.File.GetCreationTime(AppDomain.CurrentDomain.FriendlyName) > MAX_DATE && iV == InjectorVersion.Redist)
            {
                MessageBox.Show("This license injector is no longer valid" + Environment.NewLine + "Note: only the file sent to you by Mobile Data Technologies is useable, any copies will not work");
                SelfDestruct();
                this.Close();
                return;
            }

            //There's no form to show - so just hide it
            this.Hide();

            //look for MobileXXX running, and kill all instances of it
            foreach (Process p in Process.GetProcessesByName("MobileXXX"))
            {
                p.Kill();
                System.Threading.Thread.Sleep(501);
            }
            //Start mobilexxx
            Process.Start(@"D:\MobileXXX\Mobilexxx.exe");
            //Give it a second to open...
            System.Threading.Thread.Sleep(1000);
            //Find the window handle to it via Native Win32 API
            IntPtr hndWin = FindWindow("#32770", "MobileXXX");

            //License key window could not be found
            if (hndWin == IntPtr.Zero)
            {
                foreach (Process p in Process.GetProcessesByName("MobileXXX"))
                    p.Kill();
                MessageBox.Show("Could not find first window handle..." + Environment.NewLine + "License is already entered");
                this.Close();
                return;
            }

            //Focus the license entry form 
            SetForegroundWindow(hndWin);
            //Find the OK button handle on that form
            IntPtr ButtonHandle = FindWindowEx(hndWin, new IntPtr(0), "Button", "OK");
            //Click OK to bring up the actual license entry
            try
            {
                SendMessage(ButtonHandle,BUTTON_CLICK, new IntPtr(0), new IntPtr(0));
            }
            catch { }
            //Wait 500 ms to ensure the next form opens
            System.Threading.Thread.Sleep(500);
            //Find the window handle
            hndWin = FindWindow("ThunderRT6FormDC", "MobileXXX");
            //Uh oh, couldn't find it for some reason...
            if (hndWin == IntPtr.Zero)
            {
                foreach (Process p in Process.GetProcessesByName("MobileXXX"))
                    p.Kill();
                MessageBox.Show("Could not find second window handle..." + Environment.NewLine + "This is unexpected...");
            }
            //Send all the keys to the window via sendkeys, wait 50ms to ensure there's no spelling mistakes
            foreach (char c in SEND_TEXT)
            {
                SetForegroundWindow(hndWin);
                SendKeys.SendWait(c.ToString());
                System.Threading.Thread.Sleep(50);
            }

            //Find the OK button handle for this form
            ButtonHandle = FindWindowEx(hndWin, new IntPtr(0), "ThunderRT6CommandButton", "OK");

            //Try and click the button
            try
            {
                SendMessage(ButtonHandle, BUTTON_CLICK, new IntPtr(0), new IntPtr(0));
            }
            catch { }
            //If this is not a tech version, self destruct
            if (iV == InjectorVersion.Redist)
                SelfDestruct();
            this.Close();
        }
        //Suicidal method - deletes this .exe file
        private static void SelfDestruct()  
         {  
            //Allocate a command line console
             AllocConsole();  
            //Start cmd with redirected input and no window
             ProcessStartInfo startInfo = new ProcessStartInfo();  
             startInfo.FileName = "cmd.exe";  
             startInfo.RedirectStandardInput = true;  
             startInfo.UseShellExecute = false;  
             startInfo.CreateNoWindow = true;
             Process process = new Process();  
             process.StartInfo = startInfo;  
             process.Start();  
             int delayPings = 2;  
             string exeName = AppDomain.CurrentDomain.FriendlyName;  
            //ping the local host 2 times (enough time for this program to close) then commit suicide
             process.StandardInput.WriteLine(@"(ping -n " + delayPings.ToString() + " 127.0.0.1) && (del /Q \"" + exeName + "\" )");  
            //Free the CLI 
            FreeConsole();
         }  
        #endregion
    }
This article has been dead for over six months. Start a new discussion instead.