Results 1 to 9 of 9
  1. #1
    defaulto's Avatar
    Join Date
    Aug 2017
    Gender
    male
    Posts
    461
    Reputation
    427
    Thanks
    347
    My Mood
    Angelic

    C# (Bot Programming) - Send Mouse Input to an inactive Application with given coords?

    I try my best to not ask for help in here since I am someone who can help himself most of the times by looking at some Documentations / Code Snippets. I reached some point where I got stuck and Documentations seem not to help at all. But I have hope that someone here got a solution.



    So. I am out of the house and took my laptop with me. I kinda got bored and decided to put some rework on an older project of mine.
    The Project itself was a Program that did collect in-game Money. It is working by clicking on hardcoded Coordinates Onscreen. That's where I thought I could make this a bit more user-friendly since while the Bot is running, the COMP can't be used anymore for anything else.

    I've heard that AutoIt can perform such things stealthy in the background by having handles to the Object Instances and sending Messages to it (perform a click, etc.). That made me sleepless the last two nights. I did some research and once I thought I could start using Microsoft Spy++ to get the Class Names I encountered a problem. The closest I can get is the ToolbarWindow32 Class (Blue), but I need both Childrens (Red).



    Even if I could solve the problem on WPE Pro, I still would have a problem with the Game itself because you can't get any further than the PluginContainer. That's why I need to work with coordinates again, but it doesn't work like that when you send the "Messages" with the Window handles. I've tried several methods already. Some didn't work and some just click at that Dekstop position (-> doesn't work if the window is not visible).


    The best looking try was this one. Yet, it isn't what I am aiming for. It just gets the position of the Window and clicks there. I want this to happen in the background pretty much.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _0xD3F_1
    {
        public partial class Main : Form
        {
            #region Imports
            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
            // Alternativ:
            // FindWindow(default(string), lpWindowName)
            // FindWindow((string)null, lpWindowName)
    
            [DllImport("user32.dll")]
            static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
    
            [DllImport("user32.dll")]
            internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
            #endregion
    
            #region Structs
            #pragma warning disable 649
            internal struct INPUT
            {
                public UInt32 Type;
                public MOUSEKEYBDHARDWAREINPUT Data;
            }
    
            [StructLayout(LayoutKind.Explicit)]
            internal struct MOUSEKEYBDHARDWAREINPUT
            {
                [FieldOffset(0)]
                public MOUSEINPUT Mouse;
            }
    
            internal struct MOUSEINPUT
            {
                public Int32 X;
                public Int32 Y;
                public UInt32 MouseData;
                public UInt32 Flags;
                public UInt32 Time;
                public IntPtr ExtraInfo;
            }
    
            #pragma warning restore 649
            #endregion
    
            #region Coords
            // Removed since I wanted to do a full rework.
            #endregion
    
            public Main()
            {
                InitializeComponent();
            }
    
            private void Main_Load(object sender, EventArgs e)
            {
                // Removed since I wanted to do a full rework.
            }
    
            private void Start_Click(object sender, EventArgs e)
            {
                IntPtr windowHandle = FindWindow(default(string), "Snipping Tool");
                ClickOnPoint(windowHandle, new Point(5, 5));
            }
    
            // Removed since I wanted to do a full rework.
    
            public static void ClickOnPoint(IntPtr wndHandle, Point clientPoint)
            {
                var oldPos = Cursor.Position;
    
                /// get screen coordinates
                ClientToScreen(wndHandle, ref clientPoint);
    
                /// set cursor on coords, and press mouse
                Cursor.Position = new Point(clientPoint.X, clientPoint.Y);
    
                var inputMouseDown = new INPUT();
                inputMouseDown.Type = 0; /// input type mouse
                inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down
    
                var inputMouseUp = new INPUT();
                inputMouseUp.Type = 0; /// input type mouse
                inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up
    
                var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
                SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    
                /// return mouse 
                Cursor.Position = oldPos;
            }
    
        }
    }
    Maybe some of you got better ideas. I appreciate any help. It's just a free-time project, but an upgrade to it would be sick.
    All I know is that there are people out there who sell their "premium Bots" for MMORPG's or whatever game they made it for and those work perfectly fine in the background. So it must be somehow possible. I just don't get it at this time. Maybe I can oversleep it and I suddenly get an idea. :D

    I accept everything as long it can run in the background, so the user can watch Netflix & co. while his in-game money rises.
    I can even jump to another coding/scripting language if you got a solution technique that works there.

    As always. I will credit everyone who tries to help me. That's the least I can do.
    Once I got a solution. I also will add it to my Rythm Game Bots - because why not? ^^
    Last edited by defaulto; 09-04-2019 at 01:34 AM.

    #LOGS
    12-02-2020 ⌨ [MPGH]defaulto got gifted the Premium Membership from [MPGH]Azuki - sponsored by [MPGH]Flengo.
    27-11-2019 ⌨ [MPGH]defaulto captured the GFX Team Base together with [MPGH]Howl & [MPGH]Poonce.
    08-14-2017 ⌨ defaulto joined the game.

  2. #2
    Disturbed's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    10,472
    Reputation
    1267
    Thanks
    2,587
    Removed because I'm pretty sure I missed something here.
    Last edited by Disturbed; 09-06-2019 at 08:54 AM. Reason: wrong, pretty sure


  3. The Following User Says Thank You to Disturbed For This Useful Post:

    defaulto (09-06-2019)

  4. #3
    Threadstarter
    Team Ehrenlos Co-Founder
    Premium Member
    defaulto's Avatar
    Join Date
    Aug 2017
    Gender
    male
    Posts
    461
    Reputation
    427
    Thanks
    347
    My Mood
    Angelic
    Aight, thanks for the long detailed answer @Disturbed. This is a big help to me and a bigger "oh rip" moment. Kernel programming is actually an option I can accept. Though I am not really a fan of it. And I think I won't go that far for a Software that got made for a dead game. However, the learning factor is attractive to me at this point. I guess I can't get around it if I want it exactly to be how I thought it should be. That's a pity.

    Is there really no other - maybe not exactly the same result - method you know? I think I would be fine with something that doesn't unfocus my current used window. I got three Screens for my Desktop PC. And planning to buy and install another one. A few Windows opened on one of them wouldn't be the world end to me, I guess. If you are as clueless as me at this point, no problem. You helped me more than each of those methods I tried to use combined.

    - Still collecting Ideas. Don't close the thread, Mike, thank you! <3 -
    Last edited by defaulto; 09-06-2019 at 02:30 AM.

    #LOGS
    12-02-2020 ⌨ [MPGH]defaulto got gifted the Premium Membership from [MPGH]Azuki - sponsored by [MPGH]Flengo.
    27-11-2019 ⌨ [MPGH]defaulto captured the GFX Team Base together with [MPGH]Howl & [MPGH]Poonce.
    08-14-2017 ⌨ defaulto joined the game.

  5. #4
    Disturbed's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    10,472
    Reputation
    1267
    Thanks
    2,587
    Quote Originally Posted by defaulto View Post
    Aight, thanks for the long detailed answer @
    Quote Originally Posted by defaulto View Post
    Disturbed. This is a big help to me and a bigger "oh rip" moment. Kernel programming is actually an option I can accept. Though I am not really a fan of it. And I think I won't go that far for a Software that got made for a dead game. However, the learning factor is attractive to me at this point. I guess I can't get around it if I want it exactly to be how I thought it should be. That's a pity.

    Is there really no other - maybe not exactly the same result - method you know? I think I would be fine with something that doesn't unfocus my current used window. I got three Screens for my Desktop PC. And planning to buy and install another one. A few Windows opened on one of them wouldn't be the world end to me, I guess. If you are as clueless as me at this point, no problem. You helped me more than each of those methods I tried to use combined.

    - Still collecting Ideas. Don't close the thread, Mike, thank you! <3 -
    Hey, I'm pretty sure I misunderstood what you were trying to do last night. I don't think my answer is relevant. My mind jumped straight to "fuck directly with the kernel and intercept events" when Windows should be able to do what you want with appropriate permissions.

    Don't drink and post about code.

    It looks like you might be trying to click a window that just isn't focused, while the snipping tool is actually minimized. It's essentially moving the mouse, activating the window, clicking it, then moving it back and activating the window. It's trying to click a background window that's not there.
    Last edited by Disturbed; 09-06-2019 at 09:21 AM. Reason: clarification


  6. #5
    Threadstarter
    Team Ehrenlos Co-Founder
    Premium Member
    defaulto's Avatar
    Join Date
    Aug 2017
    Gender
    male
    Posts
    461
    Reputation
    427
    Thanks
    347
    My Mood
    Angelic
    Quote Originally Posted by Disturbed View Post
    It looks like you might be trying to click a window that just isn't focused, while the snipping tool is actually minimized. It's essentially moving the mouse, activating the window, clicking it, then moving it back and activating the window. It's trying to click a background window that's not there.
    Well, you were right with your first answer and what I wanted to do. I am just okay with something like this instead (because I got enough Screens for it) ^.

    Gotta look at some Documentation now because there probably is this thing that you can force a Window to stay up / click on an unfocused Window without unfocusing your current one. Still. Thank you again for bothering about this even though you mentioned that you got nothing to do with C#. You are currently the only one who responded to this problem and helped me at some point. Cheers~
    Last edited by defaulto; 09-06-2019 at 04:47 PM.

    #LOGS
    12-02-2020 ⌨ [MPGH]defaulto got gifted the Premium Membership from [MPGH]Azuki - sponsored by [MPGH]Flengo.
    27-11-2019 ⌨ [MPGH]defaulto captured the GFX Team Base together with [MPGH]Howl & [MPGH]Poonce.
    08-14-2017 ⌨ defaulto joined the game.

  7. #6
    Disturbed's Avatar
    Join Date
    Feb 2009
    Gender
    male
    Posts
    10,472
    Reputation
    1267
    Thanks
    2,587
    Quote Originally Posted by defaulto View Post
    Gotta look at some Documentation now because there probably is this thing that you can force a Window to stay up / click on an unfocused Window without unfocusing your current one.
    If it's fast enough you probably won't even notice it. I'm not sure how much/how often it has to click on the other window, though.

    My C# knowledge is about 8 years out of date. I can still do OOP basics but have forgotten almost everything in the standard libraries. These days I mostly work with python or powershell. Working on learning Golang.


  8. #7
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    To do it in the background you can probably use SendMessage to send a click on that button to the processing application (See here)

    Edit: You can pass coordinates of the click using the lParam (Documentation)

    Another Edit: Here's my tested sample that clicks the Save button in the Notepad++ toolbar
    Code:
    internal class Program
        {
            [DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string windowTitle);
    
            public static void Main(string[] args)
            {
                IntPtr mainWindowHandle = Process.GetProcessesByName("notepad++")[0].MainWindowHandle;
                IntPtr anotherParent = Program.FindWindowEx(
                    mainWindowHandle,
                    IntPtr.Zero,
                    "ReBarWindow32",
                    null);
    
                IntPtr toolbarHandle = Program.FindWindowEx(
                    anotherParent,
                    IntPtr.Zero,
                    "ToolbarWindow32",
                    null);
    
                int WM_LBUTTONDOWN = 0x0201;
                int WM_LBUTTONUP = 0x0202;
                Program.SendMessage(toolbarHandle, WM_LBUTTONDOWN, IntPtr.Zero, BuildLParam(50, 20));
                Program.SendMessage(toolbarHandle, WM_LBUTTONUP, IntPtr.Zero, BuildLParam(50, 20));
            }
    
            public static IntPtr BuildLParam(ushort low, ushort high) =>
                (IntPtr)(((uint)high << 16) | (uint)low);
        }
    Last edited by Biesi; 09-09-2019 at 05:59 AM.

  9. The Following User Says Thank You to Biesi For This Useful Post:

    defaulto (09-09-2019)

  10. #8
    Threadstarter
    Team Ehrenlos Co-Founder
    Premium Member
    defaulto's Avatar
    Join Date
    Aug 2017
    Gender
    male
    Posts
    461
    Reputation
    427
    Thanks
    347
    My Mood
    Angelic
    Quote Originally Posted by Biesi View Post
    To do it in the background you can probably use SendMessage to send a click on that button to the processing application (See here)

    Edit: You can pass coordinates of the click using the lParam (Documentation)

    Another Edit: Here's my tested sample that clicks the Save button in the Notepad++ toolbar
    Code:
    internal class Program
        {
            [DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string windowTitle);
    
            public static void Main(string[] args)
            {
                IntPtr mainWindowHandle = Process.GetProcessesByName("notepad++")[0].MainWindowHandle;
                IntPtr anotherParent = Program.FindWindowEx(
                    mainWindowHandle,
                    IntPtr.Zero,
                    "ReBarWindow32",
                    null);
    
                IntPtr toolbarHandle = Program.FindWindowEx(
                    anotherParent,
                    IntPtr.Zero,
                    "ToolbarWindow32",
                    null);
    
                int WM_LBUTTONDOWN = 0x0201;
                int WM_LBUTTONUP = 0x0202;
                Program.SendMessage(toolbarHandle, WM_LBUTTONDOWN, IntPtr.Zero, BuildLParam(50, 20));
                Program.SendMessage(toolbarHandle, WM_LBUTTONUP, IntPtr.Zero, BuildLParam(50, 20));
            }
    
            public static IntPtr BuildLParam(ushort low, ushort high) =>
                (IntPtr)(((uint)high << 16) | (uint)low);
        }
    Exactly what I was looking for. Thanks for posting this. You can't think of how much you helped me with this one

    #LOGS
    12-02-2020 ⌨ [MPGH]defaulto got gifted the Premium Membership from [MPGH]Azuki - sponsored by [MPGH]Flengo.
    27-11-2019 ⌨ [MPGH]defaulto captured the GFX Team Base together with [MPGH]Howl & [MPGH]Poonce.
    08-14-2017 ⌨ defaulto joined the game.

  11. #9
    XoXlonG's Avatar
    Join Date
    Sep 2019
    Gender
    male
    Posts
    22
    Reputation
    10
    Thanks
    0
    Thanks for the useful information

Similar Threads

  1. [Help] Bot Programming - You have been suspended for running
    By ShadowPwnz in forum Epic Duel (ED) Hacks / Cheats / Trainers
    Replies: 9
    Last Post: 12-18-2011, 11:29 PM
  2. [Help Request] Jitbit only sending mouse commands!
    By berryman13 in forum Vindictus Help
    Replies: 4
    Last Post: 05-14-2011, 06:02 PM
  3. [Info] The Controversy between botting programs.
    By lockdown6435 in forum Runescape Hacks / Bots
    Replies: 7
    Last Post: 02-14-2011, 10:06 AM
  4. [Request] bot,program or script
    By popuna4o in forum Hack Requests
    Replies: 0
    Last Post: 11-13-2010, 10:57 AM
  5. (WLO) Wonderland Online Bot program
    By ryudo98 in forum Suggestions, Requests & General Help
    Replies: 4
    Last Post: 09-13-2009, 10:52 PM