access violation, anyone know why?

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
VOID KeyControl (PVOID pvoid)
{
	POINT cursor[200];
	double duration;
	clock_t start,
			finish;

	int i = 0,
		current,
		mid,
		midStatic,
		block = 0;

	for (i = 0; i < 200; ++i)
	{
		KeyboardControll();
		GetCursorPos(&cursor[i]);
		Sleep (15);
	}

	mid = (200 / 2) - 1;
	midStatic = mid;

	do
	{
		duration = 0;
		i=0;
		while (i < 200)
		{
			if (duration == 0)
				start = clock();
			
			if (AutoScrollOn)
			{
				newAScroll (&ehwnd);
			}

			if ( i == 0)		// i is used for reading 2seconds ago cursor position
				current = 200 - 1;		//sets j to read most recent Cursor array
			else
				current = i - 1;

			if ( i > midStatic )
				mid = i % 100;
			else
				++mid;

			if (CompareCursor (cursor[i], cursor[current]))
			{
				if (CompareCursor (cursor[mid], cursor[current]))
				{
					if (block == 0)
					{
						block = 200;	//disable click for 1second after clicking
						PostMessage (ehwnd, WM_USER_CLICK, 0, 0);
					}
					else
						--block;
				}
				else
				{
					if (block != 0)
						--block;
				}
			}
			else
			{
				if (block != 0)
					--block;
			}

			finish = clock();
			duration = (double)(finish - start) /CLOCKS_PER_SEC;
			if (duration >= 0.015)
			{
				++i;
				KeyboardControll();
				GetCursorPos(&cursor[i]);
				duration = 0;
			}
		}//end of for loop
	}while(TRUE);
}


the code for compare
1
2
3
4
5
6
7
8
9
10
11
12
bool CompareCursor (POINT Previous, POINT Current)
{
	bool flag = FALSE;

	if ( (Current.x > ( Previous.x - 25 )) && (Current.x < ( Previous.x + 25 )) &&
		 (Current.y > ( Previous.y - 25 )) && (Current.y < ( Previous.y + 25 ))		)
	{
		flag = TRUE;
	}

	return flag;
}


there is only violation when it goes into the comparecursor function of mine.
It's not obvious why it's crashing, but if it's crashing in CompareCursor, it's probably caused by bad indexing into cursor.

I'd suggest running it in a debugger. When it crashes, have a look at the stack and check the index values into cursor, then try to work out why they're wrong.
yup, just ran it through debugger. its seems that even though i made a statement
1
2
3
4
if ( i > midStatic )
				mid = i % 100;
			else
				++mid;

but for some reason mid will exceed value 199. which it shouldnt. the values i want for mid should be between 0 and 199.

is there something wrong with this part of code?

thx
mid starts off at 99.
midStatic starts of at 99.

i is always less than midStatic, so mid is always incremented

By the time i gets to 101, mid is 200, making it out of bounds.
i altered the code and did the debugging again, had a look at the values it produces and they are fine. But when i run it like a normal, access violation happens again and its still mid > 200.

so i made a loop to fix the error, but that doesnt solve why mid exceeds 200.
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
VOID KeyControl (PVOID pvoid)
{
#define CursorArray 200

	bool StartTimer = TRUE;
	POINT cursor[CursorArray];
	double duration;
	clock_t start,
			finish;

	int CursorNumber,
		current,
		mid,
		midStatic,
		block = 0;

	for (int j = 0; j < CursorArray; ++j)
	{
		KeyboardControll();
		GetCursorPos(&cursor[j]);
		Sleep (10);
	}

	mid = (200 / 2) - 1;
	midStatic = mid;

	do
	{
		//for (int i = 0; i < 200; ++i)
		//{
		duration = 0;
		CursorNumber = 0;
		while (CursorNumber < CursorArray)
		{
			if (StartTimer)
			{
				StartTimer = FALSE;
				start = clock();
			}
			
			if (AutoScrollOn)
			{
				/*newAutoVScroll(&ehwnd);
				newAutoHScroll(&ehwnd);*/
				newAScroll (&ehwnd);
			}

			if (duration >= 15)
			{

				if ( CursorNumber == 0)		// i is used for reading 2seconds ago cursor position
					current = 200 - 1;		//sets j to read most recent Cursor array
				else
					current = CursorNumber - 1;

				do
				{
					if ((mid > CursorArray) || (mid < 0))
						mid = 0;

					if ( CursorNumber > midStatic )
						mid = CursorNumber % 100;
					else
						++mid;
				}
				while( (mid > CursorArray) || (mid < 0) );	//loop for error checking

				if (CompareCursor (cursor[CursorNumber], cursor[current]))
				{
					if (CompareCursor (cursor[mid], cursor[current]))
					{
						if (block == 0)
						{
							block = 200;	//disable click for 2second after clicking
							PostMessage (ehwnd, WM_USER_CLICK, 0, 0);
						}
						else
							--block;
					}
				}
				++CursorNumber;
				KeyboardControll();
				GetCursorPos(&cursor[CursorNumber]);
				SetCursorPos (cursor[CursorNumber].x, cursor[CursorNumber].y);
				duration = 0;
				StartTimer = TRUE;
			}
			if (KeyTerminate)
				return;
			
			Sleep (5);
			finish = clock();
			duration = finish - start;
		}//end of for loop
	}while(TRUE);
}
I can't comment on your algorithm because I don't know what you're trying to do.
i am trying to write a program that does automatic mouse clicks. so what i am trying to do is sample 200 mouse positions and compare the current, the earliest and one in between the two mouse positions.

that is where the i, mid and midStatic comes in
Topic archived. No new replies allowed.