You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Koffi调用Win32 API时FindWindowW无法找到Progman窗口

解决FindWindowW调用返回0的问题

问题根源分析

你的代码里有两个关键问题导致FindWindowW无法正确找到Progman窗口:

  1. 函数签名参数名拼写错误:lpClassNmae应为lpClassName,虽Koffi对参数名要求不严格,但错误命名可能引发绑定歧义;
  2. 返回值类型不匹配:Windows的HWND在64位系统下是64位指针类型,用int作为返回值会导致指针被截断,返回错误的0值。

修正步骤

  • 修正函数签名:使用Koffi内置的hwnd类型(专门对应窗口句柄)或ptr类型作为返回值,同时修正参数名拼写:
const FindWindowW = user32.func("hwnd FindWindowW(str lpClassName, str lpWindowName)");
  • 优化调用参数:查找窗口时可以只指定类名,忽略窗口名(传null),避免因系统本地化或窗口名变动导致查找失败:
const progman = FindWindowW('Progman', null);
  • 64位环境兼容:hwnd或ptr类型会自动适配32/64位系统,避免指针截断问题。

修正后的完整代码片段

const FindWindowW = user32.func("hwnd FindWindowW(str lpClassName, str lpWindowName)");

export function attachToWallpaper(targetHwnd: number | bigint) {
    // 步骤 1: 找到桌面顶层管理器 Progman
    const progman = FindWindowW('Progman', null);
    console.log('Progman:', progman);
    if (progman === 0 || progman === null) {
        console.error('X Couldn\'t find Progman window');
        return false;
    }
    // 步骤 2: 发送 0x052C 消息
    const resultPtr = koffi.alloc('int', koffi.sizeof('int'));
    SendMessageTimeoutW(progman, WM_SPAWN_WORKERW, 0, 0, SMTO_NORMAL, 1000, resultPtr);
    koffi.free(resultPtr);
}

内容的提问来源于stack exchange,提问作者Z_Yiri_C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 20:54:50