示例#1
0
        protected override void OnSendInput(IntPtr hwnd, string username, string password)
        {
            #region Validation
            if (hwnd == IntPtr.Zero)
            {
                throw new ArgumentException("Hwnd", "Invalid window handle specified.");
            }
            if (String.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentNullException("Username", "Invalid username specified.");
            }
            if (String.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("Password", "Invalid password specified.");
            }
            #endregion

            //get window info
            WindowInfo window = new WindowInfo(hwnd);

            //check if window is valid
            if (window.IsValidWindow)
            {

                try
                {
                    //block user input
                    User32.BlockInput(true);

                    //restore window (case it minimized)
                    window.Restore(false);

                    //bring window to front
                    window.BringToFront();

                    //activate window
                    window.Activate();

                    //create input simulator
                    WindowsInput.KeyboardSimulator sim = new WindowsInput.KeyboardSimulator();

                    //send tab
                    sim.KeyDown(WindowsInput.Native.VirtualKeyCode.TAB);

                    //clear username filed
                    sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                    //send back to clear any possible typed value
                    sim.KeyDown(WindowsInput.Native.VirtualKeyCode.BACK);

                    //send username
                    sim.TextEntry(username);

                    //initiate auth
                    sim.KeyDown(WindowsInput.Native.VirtualKeyCode.RETURN);

                    //wait for login
                    if (this.SendWaitTime > 0)
                    {
                        System.Threading.Thread.Sleep(this.SendWaitTime);
                    }

                    //clear password filed
                    sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                    //send back to clear any possible typed value
                    sim.KeyDown(WindowsInput.Native.VirtualKeyCode.BACK);

                    //send bassword
                    sim.TextEntry(password);

                    //initiate login
                    sim.KeyDown(WindowsInput.Native.VirtualKeyCode.RETURN);

                    if (this.SendWaitTime > 0)
                    {
                        //sleep few seconds to allow login
                        System.Threading.Thread.Sleep(this.SendWaitTime);
                    }
                }
                catch
                {
                    //ignore
                }
                finally
                {

                    //unblock input
                    User32.BlockInput(false);
                }
            }
        }
示例#2
0
文件: Uplay.cs 项目: GAMP/Plugins
        public override void Install(IApplicationLicense license, IExecutionContext context, ref bool forceCreation)
        {
            if (context.HasCompleted | context.AutoLaunch)
            {
                #region Validation
                //get installation directory
                string uplayPath = this.GetUplayPath();

                if (String.IsNullOrWhiteSpace(uplayPath) || !File.Exists(uplayPath))
                {
                    context.Client.Log.AddError(String.Format("Uplay client executable not found at {0}.", uplayPath), null, LogCategories.Configuration);
                    return;
                }

                #endregion

                string processName = Path.GetFileNameWithoutExtension(uplayPath);

                #region Initialize Process

                //get existing uplay process
                var uplayProcess = Process.GetProcessesByName(processName).Where(x => String.Compare(x.MainModule.FileName, uplayPath, true) == 0).FirstOrDefault();

                bool processExisted = uplayProcess != null;

                if(!processExisted)
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.FileName = uplayPath;
                    startInfo.WorkingDirectory = Path.GetDirectoryName(uplayPath);
                    startInfo.ErrorDialog = false;
                    startInfo.UseShellExecute = false;

                    //create uplay process
                    uplayProcess = new Process() { StartInfo = startInfo };
                }

                uplayProcess.EnableRaisingEvents = true;
                uplayProcess.Exited += new EventHandler(OnInternalProcessExited);

                #endregion

                #region Start Uplay
                if (uplayProcess.Start())
                {
                    //mark process created
                    forceCreation = true;

                    //atach handlers
                    context.ExecutionStateChaged += OnExecutionStateChaged;

                    //add process to context process list
                    context.AddProcess(uplayProcess, true);

                    if (CoreProcess.WaitForWindowCreated(uplayProcess, 120000, true))
                    {
                        try
                        {
                            //disable input
                            User32.BlockInput(true);

                            //get window
                            WindowInfo uplayWindow = new WindowInfo(uplayProcess.MainWindowHandle);

                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //give some time to activate fields
                            System.Threading.Thread.Sleep(5000);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //create input simulator
                            WindowsInput.KeyboardSimulator sim = new WindowsInput.KeyboardSimulator();

                            //send tab
                            sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.TAB);

                            //clear username filed
                            sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //send back to clear any possible typed value
                            sim.KeyDown(WindowsInput.Native.VirtualKeyCode.BACK);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //set username
                            sim.TextEntry(license.KeyAs<UserNamePasswordLicenseKeyBase>().Username);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //swicth field
                            sim.KeyDown(WindowsInput.Native.VirtualKeyCode.TAB);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //clear password filed
                            sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //send back to clear any possible typed value
                            sim.KeyDown(WindowsInput.Native.VirtualKeyCode.BACK);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //set password
                            sim.TextEntry(license.KeyAs<UserNamePasswordLicenseKeyBase>().Password);

                            //disable input
                            User32.BlockInput(true);
                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //proceed with login
                            sim.KeyDown(WindowsInput.Native.VirtualKeyCode.RETURN);

                            //disable input
                            User32.BlockInput(true);

                            //activate origin window
                            uplayWindow.BringToFront();
                            uplayWindow.Activate();

                            //set environment variable
                            Environment.SetEnvironmentVariable("LICENSEKEYUSER", license.KeyAs<UplayLicenseKey>().Username);

                            //delay installation process
                            System.Threading.Thread.Sleep(5000);
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            //enable input
                            User32.BlockInput(false);
                        }

                    }
                    else
                    {
                        context.Client.Log.AddError("Uplay client window was not created after specified period of time.", null, LogCategories.Configuration);
                    }
                }
                else
                {
                    context.Client.Log.AddError(String.Format("Uplay client executable {0} could not be started.", uplayPath), null, LogCategories.Configuration);
                }
                #endregion
            }
        }
示例#3
0
文件: Origin.cs 项目: GAMP/Plugins
        private bool FocusField(Process originProcess, OriginInputFileds field, int tries)
        {
            WindowInfo window = new WindowInfo(originProcess.MainWindowHandle);
            WindowsInput.KeyboardSimulator sim = new WindowsInput.KeyboardSimulator();

            for (int i = 0; i < tries; i++)
            {
                try
                {
            #if !DEBUG
                    User32.BlockInput(true);
            #endif

                    //reactivate window
                    window.BringToFront();
                    window.Activate();

                    //create input variables
                    OriginInputFileds focusedField = OriginInputFileds.None;
                    OriginInputFileds checkedField = OriginInputFileds.None;

                    bool aero_on = false;

                    //check if aero is on
                    if (Environment.OSVersion.Version.Major > 5)
                        Win32API.Modules.DwmApi.DwmIsCompositionEnabled(out aero_on);

                    //grab snapshot
                    var image = aero_on ? Imaging.CaptureScreenRegion(originProcess.MainWindowHandle) : Imaging.CaptureWindowImage(originProcess.MainWindowHandle);

                    if (this.GetFields(image, out focusedField, out checkedField))
                    {
                        if (focusedField == field)
                            return true;
                    }

                    //send tab
                    sim.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);

                    //pause
                    System.Threading.Thread.Sleep(250);
                }
                catch
                {
                    throw;
                }
                finally
                {
            #if !DEBUG
                    User32.BlockInput(false);
            #endif
                }
            }
            return false;
        }
示例#4
0
文件: BattleNet.cs 项目: GAMP/Plugins
        public override void Install(IApplicationLicense license, IExecutionContext context, ref bool forceCreation)
        {
            if (context.HasCompleted | context.AutoLaunch)
            {
                if(cToken!=null && cToken.Token.CanBeCanceled)
                {
                    cToken.Cancel();
                }
                else
                {
                    cToken = new CancellationTokenSource();
                }

                var settings = this.SettingsAs<BattleNetManagerSettings>();
                var key = license.KeyAs<BattleNetLicenseKey>();

                #region VALIDATION

                if (settings == null)
                    throw new ArgumentNullException("Settings");

                if (key == null)
                    throw new ArgumentNullException("Key");

                if (string.IsNullOrWhiteSpace(key.Username))
                    throw new ArgumentNullException("Username");

                if (string.IsNullOrWhiteSpace(key.Password))
                    throw new ArgumentNullException("Password");

                #endregion

                //get full path to executable
                string fulleExePath = Environment.ExpandEnvironmentVariables(context.Executable.ExecutablePath);

                //if working directory not specified set it to null
                string workingDirectory = string.IsNullOrWhiteSpace(context.Executable.WorkingDirectory) ? null : Environment.ExpandEnvironmentVariables(context.Executable.WorkingDirectory);

                string userName = key.Username;
                string passWord = key.Password;

                #region VALIDATE FILE EXISTS
                if (!File.Exists(fulleExePath))
                {
                    context.WriteMessage(string.Format("BattleNet executable not found at {0}", fulleExePath));
                    return;
                }
                #endregion

                #region PROCESS CLEANUP
                //ensure no battlenet processes running
                foreach (var processModuleFileName in processImageFileNames)
                {
                    if (string.IsNullOrWhiteSpace(processModuleFileName))
                        continue;

                    var processList = Process.GetProcessesByName(processModuleFileName);
                    processList.ToList().ForEach(x =>
                    {
                        try
                        {
                            x.Kill();
                        }
                        catch
                        {
                            Trace.WriteLine(string.Format("Could not kill BattleNet process {0}", processModuleFileName));
                        }
                    });
                }
                #endregion

                #region CONFIG CLEANUP
                //ensure configuration valid for login operation
                if (File.Exists(configPath))
                {
                    var fullConfig = File.ReadAllText(configPath);
                    fullConfig = RemoveLineForPattern(fullConfig, @"AutoLogin");
                    fullConfig = RemoveLineForPattern(fullConfig, @"SavedAccountNames");
                    File.WriteAllText(configPath, fullConfig);
                }
                #endregion

                //create process start info
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = fulleExePath;
                startInfo.WorkingDirectory = workingDirectory;

                //startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //startInfo.CreateNoWindow = true;

                //create process class
                Process bnProcess = new Process() { StartInfo = startInfo };

                //try to start process and add it to execution context
                if (context.AddProcessIfStarted(bnProcess,true))
                {
                    //assign event handler
                    context.ExecutionStateChaged += OnExecutionStateChaged;

                    //enable event raising and hook event handlers
                    bnProcess.EnableRaisingEvents = true;
                    bnProcess.Exited += OnInternalProcessExited;

                    //wait for battlenet window
                    var task = WindowInfo.WaitForWindowCreatedAsync("Battle.net Login", -1,cToken.Token );

                    try
                    {
                        task.Wait();
                    }
                    catch (AggregateException aex)
                    {
                        aex.Handle(ex =>
                        {
                            // Handle the cancelled tasks
                            TaskCanceledException tcex = ex as TaskCanceledException;
                            if (tcex != null)
                            {
                                Console.WriteLine("Handling cancellation of task {0}", tcex.Task.Id);
                                return true;
                            }

                            // Not handling any other types of exception.
                            return false;
                        });
                        return;
                    }

                    if (!task.Result.Item1)
                    {
                        //window not found
                        return;
                    }

                    Process targetProcess = task.Result.Item2.First();

                    #region SIM
                    try
                    {

                        IntPtr window = targetProcess.MainWindowHandle;

                        WindowInfo info = new WindowInfo(window);

                        if (info.IsMinimized)
                            info.Restore();

                        info.BringToFront();
                        info.Activate();

                        //block user input
                        User32.BlockInput(true);

                        Thread.Sleep(5000);

                        KeyboardSimulator sim = new KeyboardSimulator();
                        MouseSimulator msim = new MouseSimulator();

                        var x = info.Location.X + info.Width - 150;
                        var y = info.Location.Y + 150;
                        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);
                        msim.LeftButtonClick();

                        //clear username filed
                        sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                        //send back to clear any possible typed value
                        sim.KeyPress(WindowsInput.Native.VirtualKeyCode.BACK);

                        //set username
                        sim.TextEntry(userName);

                        //reactivate
                        info.BringToFront();
                        info.Activate();

                        x = info.Location.X + info.Width - 150;
                        y = info.Location.Y + 200;
                        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);

                        msim.LeftButtonClick();

                        //clear password filed
                        sim.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LCONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);

                        //send back to clear any possible typed value
                        sim.KeyPress(WindowsInput.Native.VirtualKeyCode.BACK);

                        //set password
                        sim.TextEntry(passWord);

                        //proceed with login
                        sim.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
                    }
                    catch(Exception ex)
                    {
                        context.WriteMessage(string.Format("License installation failed {0}", ex.Message));
                    }
                    finally
                    {
                        //unblock user input
                        User32.BlockInput(false);
                    }
                    #endregion
                }
            }
        }