Here's what I'm trying to do with this WinAPI function:
EnumWindows(
_In_ WNDENUMPROC lpEnumFunc,
_In_ LPARAM lParam
);
It is called from the FeedWindowsHandlesList function of this class WinFinder (simplified here).
class WinFinder
{
WinFinder();
~WinFinder();
std::vector<HWND> winHandles;
void FeedWindowsHandlesList();
};
This FeedWinDowsHandlesList function calls EnumWindows, which in turn triggers a callback for every handle found. There, the HWND is added to the winHandles member of the calling WinFinder instance. The challenge for me is to access members of the calling WinFinder instance. I tried two methods, they both fail the same way.
Method1:(Inspired from this SO post)
Calling function:
void WinFinder::FeedWindowsHandlesList() {
LPARAM param = reinterpret_cast<LPARAM>(this);
EnumWindows(EnumWindowsProc, param);
}
Callback(simplified):
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
WinFinder thisWF = *(reinterpret_cast<WinFinder*>(lParam));
thisWF.winHandles.push_back(hWnd);
return TRUE;
}
A breakpoint at the push_back level lets me see that adding occurs, and the size of the vector gets to 1. But the next time I get in the callback, the vector is empty. When EnumWindows has finished, the vector is totally empty.
I also tried like this
Method2:
Calling function:
void WinFinder::FeedWindowsHandlesList() {
WinFinder * wf =this;
EnumWindows(EnumWindowsProc,(LPARAM)wf);
}
Callback:
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
WinFinder thisWF= *((WinFinder*)lParam);
thisWF.winHandles.push_back(hWnd);
return TRUE;
}
So, How do you think I could do to access the vector member of the WinFinder class that calls EnumWindows, without losing anything?