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

筅森魡賤 提交于 2021-02-17 04:43:27

问题


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!


回答1:


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);
}



回答2:


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



来源:https://stackoverflow.com/questions/44528390/winapi-enumwindowsproc-non-standard-syntax-use-to-create-a-point-to-a-member

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!