3

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)

2 Answers2

3

If you intended to emulate a member function you should create an instance of the cursor before trying to use it:

Cursor cursor;
cursor.SetCursorPosFunction(x, y);

You can also add a constructor to your class to initialize the pointer:

class Cursor
{
public:
    Cursor()
        : SetCursorPosFunction((_SetCursorPos) GetProcAddress(...))
    { }
    typedef BOOL(__stdcall *_SetCursorPos) (int X, int Y);
    const _SetCursorPos SetCursorPosFunction;
};
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
3

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.

Kostas
  • 4,061
  • 1
  • 14
  • 32
  • Yeah, it is like that i want it, but i get some errors. Maybe i initialize it wrong? Please see updated threade for error messages. :) Thank you for taking your time to answer :D – ILikeMyBallsWarm Aug 17 '18 at 17:08
  • @ILikeMyBallsWarm You need to initialize all static members in global space. I kind of dislike this fact about C++, but you have to do it anyway. – Kostas Aug 17 '18 at 17:13
  • yep, that was my problem :D Thanks :D Have a nice day and keep rocking. PS. It should be Cursor::_SetCursorPos Cursor::SetCursorPosFunction = nullptr; – ILikeMyBallsWarm Aug 17 '18 at 17:24