2

When i play Age of empires or and other strategy game i often need to sell some resource 100 times and then by another. It's annoying :(

I wrote a program which helps me by clicking the mouse using :

//This is the decleration
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

private void ClickTheMouse()
{
      int X = Cursor.Position.X;
      int Y = Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

and then by using a timer and the code above i can click 30 times.

It all works great in and window in windows (word, Firefox, etc..) and even in Age of empires, But unfortunately it doesn't work in "Stronghold crusader" and some other games i don't remember their names.

I assume this happens because the click simulated in high level, and if it will be simulated closer to driver level it will work.

I have 2 questions :

  1. is my assumption right?
  2. how can i simulate mouse click in a lower level which will work in this game?

I use c# , but any c++ code will help also (i'll just use PInvoke).

Thanks,

Filburt
  • 17,626
  • 12
  • 64
  • 115
OopsUser
  • 4,642
  • 7
  • 46
  • 71
  • Without testing/debugging, it might be a resolution issue. This can complicate where the parameters X and Y actually reference on the screen. – Corey Ogburn Oct 04 '12 at 20:43
  • It's probably not a location issue, because it doesn't click anywhere on the screen. – OopsUser Oct 04 '12 at 20:51
  • If it's DirectInput, look at [this question](http://stackoverflow.com/questions/3644881/simulating-keyboard-with-sendinput-api-in-directinput-applications) – Corey Ogburn Oct 04 '12 at 22:35
  • You could use Spy++ to see what messages does the game receive when you click. Do this with your mouse, then with C#, then compare results. – skink Oct 05 '12 at 14:59

2 Answers2

1

I faced the same problem at one point. Are you trying to use the application through the release/debug compiler in VS2010? If so, try running the individual compiled file on its own, and remember to run it as an admin if applicable.

My bots refused to work correctly in certain games when hosted by VSHost for whatever reason.

CellPunk
  • 11
  • 1
  • I tried to run the app from the release folder, it sends the clicks (i know it because it works in word/notepad/explorer), the game just doesn't respond to them. some one told me it might be because the game uses "Direct input" – OopsUser Oct 04 '12 at 21:26
  • Thank you very much. "as admin" makes it work. – JHK Apr 25 '21 at 05:50
0

You can try to use the SendInput function n User32.dll:

http://alala666888.wordpress.com/2010/09/17/simulate-mouse-event-using-sendinput-and-setcursorpos/

However, you won't be able to inject a input event inside an application with a greater integrity level of yours. I think this is happening to you.

user229044
  • 232,980
  • 40
  • 330
  • 338
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50