• dll中获取当前窗口的句柄


    在dll中,我们或多或少的需要获取加载dll的当前窗口的句柄

    下面就有一种方法:

    通过EnumWindows枚举所有窗口在比较传入的当前进程的PID与枚举的进程的线程ID。

    如果相同,这就返回枚举到的句柄,并结束枚举

    GetWindowThreadProcessId哪个线程创建了这个窗口,返回的就是这个线程的id号。

    代码如下

    #include <windows.h>
    
    HWND GetWindowHandle();
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
    
    BOOL WINAPI DllMain(
                        HINSTANCE hinstDLL,  // handle to the DLL module
                        DWORD fdwReason,     // reason for calling function
                        LPVOID lpvReserved   // reserved
                        )
    {
    
        switch(fdwReason)
        {
        case DLL_PROCESS_ATTACH:
            MessageBox(GetWindowHandle(),"HSDH", "DSBUDF", MB_ICONWARNING);
            break;
        default:
            break;
        }
    
        return TRUE;
    }
    
    HWND GetWindowHandle()// 获取创建偶句柄
    {
        DWORD dwCurrentProcessId = GetCurrentProcessId();
    
        if(!EnumWindows(EnumWindowsProc, (LPARAM)&dwCurrentProcessId))
        {
            return (HWND)dwCurrentProcessId;
        }
    
        return NULL;
    }
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)// 回调函数
    {
        DWORD dwCurProcessId = *((DWORD*)lParam);
        DWORD dwProcessId = 0;
    
        GetWindowThreadProcessId(hwnd, &dwProcessId);
        if(dwProcessId == dwCurProcessId && GetParent(hwnd) == NULL)
        {
            *((HWND *)lParam) = hwnd;
            return FALSE;
        }
    
        return TRUE;
    }
    

      ddtttttt

  • 相关阅读:
    阿里云服务器,http免费转https详细教程
    springboot系列总结(二)---springboot的常用注解
    springboot系列总结(一)---初识springboot
    java 查询当天0点0分0秒
    mysql各个版本驱动jar包下载 mysql/mysql-connector-java/5.1.22
    泰坦尼克 学习
    切片
    忽略warning 警告
    迭代 递归 循环 遍历
    标准化 归一化
  • 原文地址:https://www.cnblogs.com/ruingking/p/13455167.html
Copyright © 2020-2023  润新知