windows - 在 Win32 C API 中执行 `EnumWindows` 查找窗口时如何避免使用全局变量?

标签 windows winapi global-variables

在使用 EnumWindows(或 FindChildWindows)Win32 API 时如何避免使用全局变量?

我大概有以下代码:

HWND prog_hwnd;

BOOL CALLBACK ProgEnumProc(HWND hwnd, LPARAM lParam) {
    if (...) {
        // found the right hwnd, assign it to prog_hwnd;
        prog_hwnd = hwnd;
        return FALSE;
    }
    return TRUE;
}

void FindProgHwnd()
{
    EnumWindows(ProgEnumProc, 0);
}

int main()
{ 
     FindProgHwnd();
     if (prog_hwnd) {
         // found prog_hwnd, but it's global variable
     }
}

如您所见,要找到正确的 hwnd,我必须使用全局变量 prog_hwnd。我想避免使用全局变量。有办法吗?

最佳答案

将指针作为 lParam 传递给变量 (LPARAM)prog_hwnd。然后在每次调用时将其传递给回调。

在回调中,您可以通过执行 *(HWND *)lParam = ... 分配给传递的变量。

关于windows - 在 Win32 C API 中执行 `EnumWindows` 查找窗口时如何避免使用全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750486/

相关文章:

c++ - 已保存文件的 Windows 事件 Hook

windows - Docker volume - windows主机和linux容器

c++ - 从窗口的额外字节泄漏内存?

python - 如何正确使用threading.local()来传递值?

c - 在单独的 session 中运行应用程序?

c++ - 全局变量析构函数未调用,从哪里开始?

c++ - RegisterClassEx 在 64 位上参数无效(但在 32 位上工作)

c# - 如何在另一个应用程序之上绘制图形/文本

c++ - 存储所有变量的参数文件

php - 如何在函数php中向数组添加元素