Hi all!

I'm trying to write a program using the GetCursorPos() and SetCursorPos() functions. When my program isn't sleeping, I want it to set the cursor position to +1 (each axis) of what it is when I call GetCursorPos().

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    DWORD timer = strtol(lpCmdLine,NULL,10);
    LPPOINT lpPoint;
    int x;
    int y;
//    while(1)
    {
        Sleep(timer);
        GetCursorPos(lpPoint);
        x = (lpPoint->x)+1;
        y = (lpPoint->y)+1;

        SetCursorPos(x,y);
    }
    return 0;
}

I'm getting a warning saying that lpPointer has been used without having been initialized, but I thought that GetCursorPos() does initialize it. Also, if I take out the x and y assingment statements, and change SetCursorPos() to trivial ints, it works, but still gives the uninitialized error (lpPointer). Therefore, the problem could be with the assignment statements, though I figure it's more likely something I'm doing wrong with lpPoint.

I'm using VC++ 6, if that's of any consequence.

[edit] Added in the +1's.

How is the compiler supposed to know that GetCursorPos() initalizes it? It would be simply too much to ask of the compiler to actually check each function to make sure that it's initalizing any uninitalized variables. So you must do it manually, regardless of whether the function handles the variables safely.

Try something like this when creating lpPoint:

LPPOINT lpPoint = NULL;

Now the compiler sure can't complain. ;)

Thank you, that does take care of the warning. But if I try to extraxt x and y from lpPointer, the program still crashes. Must be something I'm doing wrong in regards to either how I'm handling the pointer, or how I'm trying to extract the data members from the struct.

GetCursorPos() expects a pointer of type POINT. All you are passing is a pointer to nothing.

POINT pt;
if( GetCursorPos( &pt ) != 0)
{
   // pt contains valid data.  Now you can use is however you wish

}

Whenever windows.h says a parameter to a function is a pointer, it means you must pass it a pointer to an object that you create, such as in the code snipped above. That sort of thing happens in most win32 api functions. It is ALWAYS a pointer to your object, unless the documentation in MSDN says otherwise.

You created the pointer but where is the memory allocated to it ? How do you expect the GetCursorPos() to return anything to a mere pointer to whom no memory has been allocated ?

In your declaration of LPPOINT variable do this: LPPOINT lpPoint = new POINT ; And then it should be fine.

Hope it helped, bye.

You created the pointer but where is the memory allocated to it ? How do you expect the GetCursorPos() to return anything to a mere pointer to whom no memory has been allocated ?

In your declaration of LPPOINT variable do this: LPPOINT lpPoint = new POINT ; And then it should be fine.

Hope it helped, bye.

there is no need to allocate memory like that. Just see my previous post for correct way to do it.

Thank you both. Tried both, and of course, both worked. Being a wet behind the ears newb, didn't remotely dawn that I created a dummy pointer....... which it should have, given the warning. But hey, experience gained.

>>which it should have, given the warning
All the compiler cares about is that you pass a pointer -- even a dummy pointer will satisfy that requirement. Its up to you pass a pointer to a valid object. Many win32 api functions validate the pointer is not NULL, and if it is NULL then the function returns without doing anything.

And don't forget to always check the return valid of those functions. Don't just assume they will return success because often they don't.

there is no need to allocate memory like that. Just see my previous post for correct way to do it.

But still looks like a good approach if the number of POINT 's go on increasing, I can allocate the memory dynamically and free them when they are no longer needed. But still different solutions to the same problem....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.