c++functionclasstypedefstdcall

Typedef function inside a class


I want to to be able to have my typedef function inside a class. But i dont find a way to do that. I need to scan for the address so i cant hardcode it in, therfor i need to sett the address like this SetCursorPosFunction = (_SetCursorPos)(address to function);

example:

class Cursor
{
public:

    typedef BOOL(__stdcall *_SetCursorPos) (int X, int Y);
    _SetCursorPos SetCursorPosFunction;
};

I want to be able to call the function like this Cursor::SetCursorPosFunction(x,y)

Example of what i mean.

void Function()
{
    DWORD_PTR AddressToFunctionSetCourserPos = Find(....);
    Cursor::SetCursorPosFunction = (Cursor::_SetCursorPos)(AddressToFunctionSetCourserPos ); //In final version it is going to be in a separate function where i get all the functions i need (This find() function can not be looped or called often, it is going to create lag etc.).

    Cursor::SetCursorPosFunction(1, 1);

}

I get the errors:

fatal error LNK1120: 1 unresolved externals
error LNK2001: unresolved external symbol "public: static int (__cdecl* Cursor::SetCursorPosFunction)(int,int)" (?SetCursorPosFunction@Cursor@@2P6AHHH@ZEA)

Solution

  • Modifying the function to a static will allow you to use it without have to instantiate a member first as you like:

    class Cursor
    {
    public:
        typedef BOOL(__stdcall *_SetCursorPos) (int X, int Y);
        static _SetCursorPos SetCursorPosFunction;
    };
    

    Cursor::SetCursorPosFunction(x,y) should now work (given you initialize it first).

    You also need to initialize the static member in global space. Something like Cursor::_SetCursorPos Cursor::SetCursorPosFunction = nullptr; should work. But be careful to have it in only one translation unit.