Samantha Garcia
Samantha Garcia

Reputation: 11

WINAPI EnumWindowsProc: Non-Standard Syntax; use & to create a point to a member

I keep getting an error when I call EnumWindows(EnumWindowsProc, 0); Which converts my BOOL CALLBACK selectionWindows::EnumWindowsProc(HWND hWnd, long lParam) function into a parameter.

I know it has something to do with the classes and selectionWindows:: but I can't figure it out for the life of me.

Here is the .h

#ifndef SELECTIONWINDOWS_H
#define SELECTIONWINDOWS_H

#include <windows.h>
#include "mainwindow.h"

#include <QWidget>
#include <iostream>

class selectionWindows : public QWidget
{
    Q_OBJECT

public:

    selectionWindows(MainWindow * w);
    void selectionWindows::addWindows();
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lparam);

    ~selectionWindows();

private:
        char buff[255];
    };

And the cut-out of the part I am having difficulty with:

BOOL CALLBACK selectionWindows::EnumWindowsProc(HWND hWnd, long lParam) {

    if (IsWindowVisible(hWnd))
        GetWindowTextW(hWnd, (LPWSTR) buff, 254);
    return true;

}    

void selectionWindows::addWindows() {

       EnumWindows(EnumWindowsProc, 0); //Here is the error

        for (int i = 0; i  != 254; ++i) {
            qDebug() << buff[i];
        }
    }

I have included windows.h & iostream

Thanks for the help!

Upvotes: 1

Views: 514

Answers (2)

marcinj
marcinj

Reputation: 49976

Your callback function must be static (or a free function), it cannot be a non-static class member method.

Upvotes: 3

IInspectable
IInspectable

Reputation: 51345

EnumWindows expects a free function or static class member as the WindowsEnumProc. You cannot pass a non-static class member. If you need access to a class instance from within your WindowsEnumProc, pass a pointer to it along in the call to EnumWindows. The lParam is

An application-defined value to be passed to the callback function.

This is an example implementation:

class selectionWindows : public QWidget {
    Q_OBJECT
public:
    selectionWindows(MainWindow * w);
    void selectionWindows::addWindows();
    BOOL CALLBACK EnumWindowsProc(HWND hWnd);
    static BOOL CALLBACK EnumWindowsProcStatic(HWND hWnd, LPARAM lParam);

    ~selectionWindows();
private:
    char buff[255];
};

The following code passes an instance pointer to the EnumWindows API:

void selectionWindows::addWindows() {
    EnumWindows(selectionWindows::EnumWindowsProcStatic, reinterpret_cast<LPARAM>(this));
    // ...
}

Where the EnumWindowsProcStatic forwards the call to the class instance:

BOOL CALLBACK selectionWindows::EnumWindowsProcStatic(HWND hWnd, LPARAM lParam) {
    selectionWindows* p = reinterpret_cast<selectionWindows*>(lParam);
    return p->EnumWindowsProc(hWnd);
}

Upvotes: 6

Related Questions