[Win32/C#] Simuleren Muisklik m.b.v. User32 probleem

Pagina: 1
Acties:
  • 63 views sinds 30-01-2008

  • Big4SMK
  • Registratie: September 2001
  • Laatst online: 10:09
Ik ben bezig met een programma die automatisch muiskliks voor mij uit moet kunnen voeren. Het betreft een bot die BlackJack voor mij moet gaan spelen.

Het probleem waar ik nu tegenaan loop is dat de applicatie in kwestie (ConnectToCasino) het niet doorheeft als ik een muisklik simuleer. Dit terwijl bijvoorbeeld mijn buroblad dat _wel_ doorheeft. Hier is een stukje van de code in de User32 class (zelf geschreven).

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        public static void click(int handle, Point p){
            //get the DC of the handle
            int dc = User32.getDC(handle);
            //get the coordinates of the screen
            Point rect = User32.getWindowRect(handle);
            //store cursor position
            Point oldPos = Cursor.Position;
            //set cursor position
            Cursor.Position = new Point(rect.X + p.X, rect.Y + p.Y);
            //Press mouse
            mouse_event(User32.MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
            //Release Mouse
            mouse_event(User32.MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
            //reload old position
            Cursor.Position = oldPos;
        }
        private const UInt32 MouseEventLeftDown = 0x0002;
        private const UInt32 MouseEventLeftUp = 0x0004;
        private const UInt32 MouseEventMove = 0x0001;
        private const UInt32 MouseEventAbsolute = 0x8000;

        [DllImport("user32.dll")]
        private static extern void mouse_event(UInt32 dwFlags, int dx, int dy, UInt32 dwData, IntPtr dwExtraInfo);


Hoe kan het dat ik op mijn buroblad WEL een klik kan uitvoeren, maar in dat venster niet?

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Gokje: werkt het in andere vensters wel (ook behalve je bureaublad dus)?
Want ik heb het gevoel dat je een of andere online blackjack iets probeert te neppen en het zou me niks verbazen als die software daar tegen "beveiligd" is op welke manier dan ook.

Daarnaast is dit een beetje een "wie debugt mijn code even" topic. Wat heb je zelf al geprobeerd? Wat werkt er niet? Heb je al gedebugged? Werken (zoals gezegd) andere windows wel?

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


  • Mastermind
  • Registratie: Februari 2000
  • Laatst online: 29-11 15:35
Ah, dit heb ik ook ooit eens gehad. Ik heb een programma geschreven die automatisch online Yahoo Chess speelde via een chessengine, ook via mouseclicks.

Ik heb daarvoor deze class geschreven:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;

namespace XChess
{
    class MouseController
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern long SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool GetCursorPos(ref POINT lpPoint);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
        private const UInt32 MouseEventLeftDown = 0x0002;
        private const UInt32 MouseEventLeftUp = 0x0004;
        private const UInt32 MouseEventRightDown = 0x0008;
        private const UInt32 MouseEventRightUp = 0x0010;

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hwnd);

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct POINT
        { public int x; public int y;}


        public MouseController()
        {
        }

        /// <summary>
        /// Sends a mouse click
        /// </summary>
        public void SendMouseClick()
        {
            mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
            mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
        }

        /// <summary>
        /// Sends a mouse click to X,Y, but the cursor will remain on the current position
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void SendMouseClick(int x, int y)
        {
            POINT defPnt = new POINT();
            GetCursorPos(ref defPnt);
            int curX = defPnt.x;
            int curY = defPnt.y;
            SetCursorPos(x, y);
            mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
            mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
            SetCursorPos(curX, curY);
        }

        /// <summary>
        /// Presses the left mouse button, and holds it
        /// </summary>
        public void LeftMouseButtonDown()
        {
            mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
        }

        /// <summary>
        /// Releases the left mouse button
        /// </summary>
        public void LeftMouseButtonUp()
        {
            mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
        }



        /// <summary>
        /// Presses the right mouse button and holds it
        /// </summary>
        public void RightMouseButtonDown()
        {
            mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
        }

        /// <summary>
        /// Releases the right mouse button
        /// </summary>
        public void RightMouseButtonUp()
        {
            mouse_event(MouseEventRightUp, 0, 0, 0, new System.IntPtr());
        }

        /// <summary>
        /// Sends a click of the right mouse button
        /// </summary>
        public void SendRightMouseClick()
        {
            mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
        }

        /// <summary>
        /// Sends a click of the right mouse button to the X,Y coordinate
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void SendRightMouseClick(int x, int y)
        {
            POINT defPnt = new POINT();
            GetCursorPos(ref defPnt);
            int curX = defPnt.x;
            int curY = defPnt.y;
            SetCursorPos(x, y);
            mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
            mouse_event(MouseEventRightUp, 0, 0, 0, new System.IntPtr());
            SetCursorPos(curX, curY);
        }

        

        /// <summary>
        /// Gets a Point object of the X,Y location of the mouse pointer
        /// </summary>
        /// <returns></returns>
        public System.Drawing.Point GetPointerLocation()
        {
            POINT defPnt = new POINT();
            GetCursorPos(ref defPnt);
            System.Drawing.Point p = new System.Drawing.Point();
            p.X = defPnt.x;
            p.Y = defPnt.y;
            return p;
        }

        /// <summary>
        /// Sets the X,Y location of the mouse pointer
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void SetPointerLocation(int x, int y)
        {
            SetCursorPos(x, y);
        }


        /// <summary>
        /// Gets or sets the location of the mouse pointer. Returns or requires a Point object.
        /// </summary>
        public System.Drawing.Point PointerLocation
        {
            get
            {
                return GetPointerLocation();
            }
            set
            {
                SetCursorPos(value.X, value.Y);
            }
        }


        /// <summary>
        /// Gets or sets the X location of the mouse cursor
        /// </summary>
        public int X
        {
            get
            {
                POINT defPnt = new POINT();
                GetCursorPos(ref defPnt);
                return defPnt.x;
            }
            set
            {
                POINT defPnt = new POINT();
                GetCursorPos(ref defPnt);
                SetCursorPos(value, defPnt.y);
            }
        }

        /// <summary>
        /// Gets or sets the Y location of the mouse cursor
        /// </summary>
        public int Y
        {
            get
            {
                POINT defPnt = new POINT();
                GetCursorPos(ref defPnt);
                return defPnt.y;
            }
            set
            {
                POINT defPnt = new POINT();
                GetCursorPos(ref defPnt);
                SetCursorPos(defPnt.x, value);
            }
        }



    }
}

  • Big4SMK
  • Registratie: September 2001
  • Laatst online: 10:09
RobIII schreef op woensdag 07 maart 2007 @ 23:30:
Gokje: werkt het in andere vensters wel (ook behalve je bureaublad dus)?
Want ik heb het gevoel dat je een of andere online blackjack iets probeert te neppen en het zou me niks verbazen als die software daar tegen "beveiligd" is op welke manier dan ook.

Daarnaast is dit een beetje een "wie debugt mijn code even" topic. Wat heb je zelf al geprobeerd? Wat werkt er niet? Heb je al gedebugged? Werken (zoals gezegd) andere windows wel?
In sommige vensters werkt het wel ja. Maar de vraag is dus hoe ik op een andere manier dan de huidige toch een klik kan doen. Het kan best dat de software daar op een of andere manier tegen beveiligd is, maar ik vind het toch vreemd dat ik met mijn muis dan wel kan klikken. Het lijkt me dat ik met software toch perfect een muisklik moet kunnen nabootsen.

Het is overigens geen "wie debug mijn code even" topic, omdat de code die ik post geen bugs bevat, deze werkt perfect, en ik krijg dan ook geen foutmeldingen. Het nadeel is alleen dat het niet precies doet wat ik ervan verwacht, dus vraag ik iemand om raad, die mij kan vertellen wat ik fout doe.

Natuurlijk heb ik zelf gekeken of het wel of niet werkt, en het blijkt dat de regel
C#:
1
mouse_event(User32.MouseEventLeftDown, 0, 0, 0, new System.IntPtr());


Niet het gewenste resultaat geeft in het debetreffende scherm, terwijl dit op het buroblad wel precies doet wat ik verwacht (namelijk precies hetzelfde als dat ik met mijn muis de linkermuisknop in druk)

@Mastermind: Volgens mij post jij precies wat ik al in mijn startpost heb beschreven. Jouw code bevat alleen nog een aantal methoden voor ander soort klikken, en voor het setten van de cursor positie, jouw techniek is niet echt anders toch? Je gebruikt precies dezelfde mouse events.

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Big4SMK schreef op donderdag 08 maart 2007 @ 15:53:
Het kan best dat de software daar op een of andere manier tegen beveiligd is
<snip>
Niet het gewenste resultaat geeft in het debetreffende scherm, terwijl dit op het buroblad wel precies doet wat ik verwacht (namelijk precies hetzelfde als dat ik met mijn muis de linkermuisknop in druk)
Tja, ik weet niet of het aan je code ligt of niet; maar ik heb nog steeds heel sterk het idee dat je een beveiliging van die app probeert te omzeilen. Die zit er natuurlijk niet voor niets in; en hier op GoT gaan wij mensen niet helpen beveiligingen omzeilen.

Als het in alle andere vensters wel werkt behalve in die ene app dan kun je er relatief veilig van op aan dat die app dus beveiligd is (of je hebt de verkeerde window handle te pakken etc.)

Dit topic is (voor overleg) voorlopig even dicht.

[ Voor 13% gewijzigd door RobIII op 08-03-2007 16:22 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Dit topic is gesloten.