I want to use this function "EnumWindows(EnumWindowsProc, NULL);". The EnumWindowsProc is a Callback function:
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
For this callback I want to use a member function of a class.
e.g:
Class MyClass
{
BOOL CALLBACK My_EnumWindowsProc(HWND hwnd, LPARAM lParam);
void test();
};
So i want to bind the called Callback with my function !!!
I try this:
void MyClass::test()
{
EnumWindowsProc ptrFunc = mem_fun(&MyClass::My_EnumWindowsProc);
EnumWindows(ptrFunc, NULL);
}
It's doesn't work, "mem_fun" can take only one argument ! Is it possible to do that ? else do you know another solution ? (maybe a solution will be possible with Boost::bind)
You need to create an Adapter, such as:
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
class MyCallback
{
public:
MyCallback() : count_(0) {};
static BOOL CALLBACK CallbackAdapter(HWND, LPARAM);
BOOL Callback(HWND);
unsigned count_;
};
BOOL MyCallback::Callback(HWND wnd)
{
char title[1025] = {};
GetWindowText(wnd, title, sizeof(title)-1);
cout << wnd << "= '" << title << "'" << endl;
++count_;
return TRUE;
}
BOOL MyCallback::CallbackAdapter(HWND wnd, LPARAM lp)
{
MyCallback* that = reinterpret_cast<MyCallback*>(lp);
return that->Callback(wnd);
}
int main()
{
MyCallback cb;
EnumWindows(&MyCallback::CallbackAdapter, reinterpret_cast<LPARAM>(&cb));
cout << "Windows Found: " << cb.count_;
return 0;
}