CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2009
    Posts
    3

    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

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  3. #3
    Join Date
    Dec 2009
    Posts
    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

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  5. #5
    Join Date
    Apr 2004
    Posts
    102

    Re: GetCursorPos returns 0

    You're not allocating any memory for lpPoint.

    Code:
    lpPoint = (LPPOINT)GlobalAlloc(GPTR, sizeof(POINT));

  6. #6
    Join Date
    Dec 2009
    Posts
    3

    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;
    }

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: GetCursorPos returns 0

    Quote Originally Posted by BobS0327 View Post
    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.

  8. #8
    Join Date
    Feb 2005
    Posts
    2,160

    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".

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: GetCursorPos returns 0

    Quote Originally Posted by BobS0327 View Post
    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.

  10. #10
    Join Date
    Apr 2004
    Posts
    102

    Re: GetCursorPos returns 0

    Quote Originally Posted by Paul McKenzie View Post
    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
    Actually, Microsoft recommends using heap functions instead of global and local functions.

  11. #11
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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
  •  





Click Here to Expand Forum to Full Width

Featured