SBSTP
SBSTP

Reputation: 3639

EnumWindows Pointer error

I'm getting a weird error trying to EnumWindows to a function inside a class. Here's my code

EnumWindows(&ConsoleDetector::EnumWindowsProc, NULL);

BOOL CALLBACK ConsoleDetector::EnumWindowsProc(HWND wnd, LPARAM lParam)
{
    char className[200];
    GetClassName(wnd, className, 200);
    if (strcmp(className, "ConsoleWindowClass"))
        m_result.push_back(wnd);
    return TRUE;
}

Here's the error im getting:

ConsoleDetector.cpp:30: error: cannot convert 'BOOL (ConsoleDetector::*)(HWND__*, LPARAM)' to 'BOOL (*)(HWND__*, LPARAM)' for argument '1' to 'BOOL EnumWindows(BOOL (*)(HWND__*, LPARAM), LPARAM)'

Using MingW. Thanks for help.

Upvotes: 1

Views: 1115

Answers (2)

Fox32
Fox32

Reputation: 13560

You can't use the windows callback function with class member functions in c++. Only static class functions or non class functions are allowed.

How should the EnumWindows function kown the instance of a class? You can only supply a function pointer, not an instance pointer to EnumWindows

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612954

You are passing an instance method. You need to pass a plain function rather than a method bound to an instance.

It has to be declared like this:

BOOL CALLBACK EnumWindowsProc(HWND wnd, LPARAM lParam)

Pass the instance of the ConsoleDetector to the lParam parameter of EnumWindows and it will in turn be passed to your callback.

Like this:

BOOL CALLBACK EnumWindowsProc(HWND wnd, LPARAM lParam)
{
    ConsoleDetector cd = static_cast<ConsoleDetector*>(lParam);
    //do stuff with cd and wnd
}

ConsoleDetector *cd = ...
EnumWindows(EnumWindowsProc, static_cast<LPARAM>(cd));

Upvotes: 3

Related Questions