|
-
December 27th, 2009, 07:10 AM
#1
GetCursorPos returns 0
Hey all,
Let me introduce myself. I'm Pepijn and I've programmed a few years in PHP. I'm trying to learn c++ now, but it's not very easy.
I wrote the following script so far, it has to display the cursors position in a dos window;
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
LPPOINT lpPoint;
int x;
int y;
if(!GetCursorPos(lpPoint))
{
cout << "An error occurred: " << endl;
cout << GetLastError();
}
else
{
x = lpPoint->x;
y = lpPoint->y;
cout << x << endl;
cout << y << endl;
}
cin.get();
return 0;
}
The problem is that GetCursorPos returns 0, but GetLastError returns 0 either.
I hope you can help.
Best regards,
Pepijn
-
December 27th, 2009, 07:23 AM
#2
Re: GetCursorPos returns 0
AN LPPOINT is a pointer to a POINT struct. However you have just a pointer and no POINT struct.
Change LPPOINT to POINT and call GetCursorPos with the address of that POINT struct
POINT point
GetCursorPos(&point)
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
December 27th, 2009, 07:31 AM
#3
Re: GetCursorPos returns 0
Code:
x = point->x;
y = point->y;
I got the following errors now:
18: base operand of `->' has non-pointer type `POINT'
19: base operand of `->' has non-pointer type `POINT'
What does that mean?
Thankyou for you help
-
December 27th, 2009, 07:33 AM
#4
Re: GetCursorPos returns 0
point.x not point->x
point is a struct. lppoint was a pointer.
You access a structs members with the dot operator
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
December 27th, 2009, 07:43 AM
#5
Re: GetCursorPos returns 0
You're not allocating any memory for lpPoint.
Code:
lpPoint = (LPPOINT)GlobalAlloc(GPTR, sizeof(POINT));
-
December 27th, 2009, 08:07 AM
#6
Re: GetCursorPos returns 0
Thx for you help!
The script is working now:
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
POINT point;
int x;
int y;
while(true) {
if(!GetCursorPos(&point))
{
cout << "An error occurred: " << endl;
cout << GetLastError();
}
else
{
x = point.x;
y = point.y;
cout << x << endl;
cout << y << endl;
}
Sleep(100);
system("cls");
}
return 0;
}
-
December 27th, 2009, 11:33 AM
#7
Re: GetCursorPos returns 0
 Originally Posted by BobS0327
You're not allocating any memory for lpPoint.
Code:
lpPoint = (LPPOINT)GlobalAlloc(GPTR, sizeof(POINT));
Code:
POINT Point;
if(!GetCursorPos(&Point))
would be simpler in this case. If you really want to put it on the heap, new would be simpler than GlobalAlloc.
-
December 27th, 2009, 11:46 AM
#8
Re: GetCursorPos returns 0
You should get out of the habit of calling it a "script" too. You're a real programmer now. You're writing a "program".
-
December 27th, 2009, 02:41 PM
#9
Re: GetCursorPos returns 0
 Originally Posted by BobS0327
You're not allocating any memory for lpPoint.
Code:
lpPoint = (LPPOINT)GlobalAlloc(GPTR, sizeof(POINT));
Please read this link:
http://www.flounder.com/badprogram.h...%20reason%20to
Even though you used GlobalAlloc, the article still is valid with respect to allocating memory when you do not need to do it.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 27th, 2009 at 02:45 PM.
-
December 27th, 2009, 09:14 PM
#10
Re: GetCursorPos returns 0
 Originally Posted by Paul McKenzie
Actually, Microsoft recommends using heap functions instead of global and local functions.
-
December 27th, 2009, 10:21 PM
#11
Re: GetCursorPos returns 0
If you wanted to show how its done with a pointer then it would have been simpler to do :-
POINT point;
LPPOINT lppoint = &point;
What is the point in dynamically allocating a POINT struct in this single function program, when a simpler stack allocated object will suffice, especially when the OP said how new to C++ he was.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|