首页 > 解决方案 > 错误 C2664 'HWND FindWindowExW (HWND, HWND, LPCWSTR, LPCWSTR)':参数 4 不能从 'bool' 转换为 'LPCWSTR'

问题描述

我在互联网上找到并稍作编辑的代码中出现此错误的原因是什么?

代码在这里:

#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <tchar.h>

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    HWND p = FindWindowEx(hwnd, NULL, _T("SHELLDLL_DefView"), false);
    HWND* ret = (HWND*)lParam;
    if (p) {
        // Gets the WorkerW Window after the current one.
        *ret = FindWindowEx(NULL, hwnd, _T("WorkerW"), NULL);
    }
    return true;
}

HWND get_wallpaper_window()
{
    // Fetch the Progman window
    HWND progman = FindWindow(_T("ProgMan"), NULL);
    // Send 0x052C to Progman. This message directs Progman to spawn a
    // WorkerW behind the desktop icons. If it is already there, nothing
    // happens.
    SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, NULL);
    // We enumerate all Windows, until we find one, that has the SHELLDLL_DefView
    // as a child.
    // If we found that window, we take its next sibling and assign it to workerw.
    HWND wallpaper_hwnd = NULL;
    EnumWindows(EnumWindowsProc, (LPARAM)&wallpaper_hwnd);
    // Return the handle you're looking for.
    return wallpaper_hwnd;
}

标签: c++winapi

解决方案


错误消息抱怨您将 a 传递bool给 的第四个参数FindWindowEx(),这是一个LPCWSTR( const wchar_t*) 指针。Abool不能隐式转换为指针。

当调用FindWindowEx()查找窗口时,您正在"SHELLDLL_DefView"传递参数。你所有的其他电话和正在通过。falselpszWindowFindWindow()FindWindowEx()NULL

所以简单地改变这个:

HWND p = FindWindowEx(hwnd, NULL, _T("SHELLDLL_DefView"), false);

为此:

HWND p = FindWindowEx(hwnd, NULL, _T("SHELLDLL_DefView"), NULL);

推荐阅读