Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use member function as first argument to EnumWindows [duplicate]

Can I use member function as first argument to EnumWindows? I don't see any workaround in this case even with boost::bind.

like image 968
FrozenHeart Avatar asked Oct 28 '25 16:10

FrozenHeart


1 Answers

Given this normal callback function:

BOOL CALLBACK EnumWindowsProc(HWND wnd, LPARAM lParam);

You can invoke EnumWindows using lParam to pass it a pointer to your class:

EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(this));

In EnumWindowsProc you can simply call member function (casting lParam to your class type). Like this:

BOOL CALLBACK EnumWindowsProc(HWND wnd, LPARAM lParam)
{
    return reinterpret_cast<MyClass*>(lParam)->EnumWindows(wnd);
}

If you don't want to make your class method public you can:

  • Pack a struct to contain both class instance and pointer to member.
  • Use a library for delegates.
  • Use boost std:bind (in this case it'll work well because you do it on your own class member, it has not to be __stdcall).

Whatever you will use you can find more details in this post here on SO.

like image 63
Adriano Repetti Avatar answered Oct 31 '25 07:10

Adriano Repetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!