3

I'm trying to use the EnumWindows function, which takes a WNDENUMPROC as callback with a lambda in order to access local variables. Unfrotunately if i try to use [&] the compiler will tell me types don't match. What i'm trying:

HWND get_wallpaper_window()
    {
    HWND progman = FindWindow(L"ProgMan", NULL);
    SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);

    HWND wallpaper_hwnd;
    EnumWindows(
            // Error here 
            [&](HWND hwnd, LPARAM lParam) -> BOOL CALLBACK 
                    {
                    HWND p = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL);
                    if (p) { wallpaper_hwnd = FindWindowEx(NULL, hwnd, L"WorkerW", NULL); }
                    }
            , NULL);
    return wallpaper_hwnd;
    }

The only solution i could come up with is making wallpaper_hwnd global and defining the lambda with [], but since it's only needed when returned from that function and not needed globally i'd rather avoid that.

I love using globals way more than people would advice, but i only do when the global variable is something that interacts with the whole program, and this is not the case.

Any better solution i'm missing?

2
  • What is the actual problem this code tries to solve? To depend on the internal window structure of the desktop like this, may very well brake in future Windows versions.
    – zett42
    Commented May 12, 2019 at 19:45
  • @zett42 customization. It's very easy to make an overly by just drawing on the generic "display" but if you need to draw on the very back that's the way.
    – Barnack
    Commented May 12, 2019 at 19:48

2 Answers 2

4

Lambda functions that capture outside variables cannot be used as Win32 callbacks. They are not convertible to raw function pointers.

The solution in your case is to pass a pointer to something as the LPARAM parameter of EnumWindows(), which is passed to the callback.

class A {...};
A a;
EnumWindows([](HWND   hwnd,LPARAM lParam) -> BOOL {
A* a = (A*)lParam;
...
},(LPARAM)&a);

Most windows functions that require a callback support passing a user-defined value which can be a pointer to a structure. The compiler is smart enough to convert a non-capturing lambda to CALLBACK (=_stdcall) calling convention automatically.

16
  • 1
    There is no reason to avoid C-style conversion when using C style functions like Win32 ones. Besides, reinterpret_cast does exactly what the C cast is doing with more typing. Commented May 12, 2019 at 18:08
  • 1
    C-style casts select the cast that best matches (static_cast, const_cast or reinterpret_cast – or a combination of). While it can be used, I personally would rather recommend using reinterpret_cast anyway. It just expresses more clearly what one actually is doing – in spite of having to write a bit more.
    – Aconcagua
    Commented May 12, 2019 at 18:13
  • 1
    Admitted, here might two oppinions clash, so it might not be worth to argue further – just let's have both presented and each reader decide on its own...
    – Aconcagua
    Commented May 12, 2019 at 18:23
  • 1
    @Barnack If they made a C++ API what about people using languages that weren't C++? Commented May 12, 2019 at 20:24
  • 1
    @bar: They already have. Now here's the deal: It's so darn complex and hard to use, that hardly anyone does. Try it, for a day, and spend the better part of it deciphering your compiler's error diagnostics. Commented May 12, 2019 at 22:17
2

Any better solution i'm missing?

Well, yes, that NULL in the second parameter is passed back to the callback so you can easily use it to propagate local state.

HWND get_wallpaper_window()
    {
    HWND progman = FindWindow(L"ProgMan", NULL);
    SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);

    HWND wallpaper_hwnd;
    EnumWindows(
            [](HWND hwnd, LPARAM lParam) -> BOOL CALLBACK 
                {
                    auto wallpaper_hwnd_ptr = reinterpret_cast<HWND*>(lParam);
                    HWND p = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL);
                    if (p) { *wallpaper_hwnd = FindWindowEx(NULL, hwnd, L"WorkerW", NULL); }
                    }
            , &wallpaper_hwnd);
    return wallpaper_hwnd;
    }
6
  • already tried, but it gives me a type error… should i reinterpret cast it?
    – Barnack
    Commented May 12, 2019 at 18:06
  • 3
    @MichaelChourdakis Maybe I'm misunderstanding the nature of HWND, but OP wants his local variable to have been updated once EnumWindows has finished, so it has to be a pointer to a HWND, no?
    – user4442671
    Commented May 12, 2019 at 18:09
  • @MichaelChourdakis, yes, and that pointer is what needs to be assigned within the callback, not the object it's pointing at.
    – user4442671
    Commented May 12, 2019 at 18:11
  • 1
    The error that he gets then is the passing of &wallpaper_hwnd, it needs probably (LPARAM)&wallpaper_hwnd. Commented May 12, 2019 at 18:13
  • that's it, the other answer got it correct @MichaelChourdakis
    – Barnack
    Commented May 12, 2019 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.