c++ - GetForegroundWindow 从 Windows 7 上的服务

标签 c++ windows-7

我正在使用服务中的 GetForegroundWindow(),因为我想监视焦点窗口,但由于交互用户问题,该函数在 Windows 7 上返回 null。有什么方法可以让我在服务中获得焦点窗口?

HWINSTA hWinStaUser = OpenWindowStation(_T("WinSta0"), FALSE, MAXIMUM_ALLOWED); 
    if (SetProcessWindowStation(hWinStaUser)) 
    {
        /* -- Open user's desktop "Default". -- */
        HDESK hDeskUser = OpenInputDesktop(0,FALSE,MAXIMUM_ALLOWED);
        //HDESK hDeskUser = OpenDesktop(_T("Default"), 0, FALSE, MAXIMUM_ALLOWED);
        /* -- Set thread desktop to "Default". -- */ 
        if (SetThreadDesktop(hDeskUser)) 
        {   
            HWND hwndActiveWin = GetForegroundWindow();

            int  idActive      = GetWindowThreadProcessId(hwndActiveWin, NULL);
            if ( AttachThreadInput(GetCurrentThreadId(), idActive, TRUE) )
            {
                HWND  hwndFocused = GetFocus ();    // focused control within the active window, i.e. focus throughout the screen
                if(hwndFocused != NULL)
                {
                    DWORD dwProcessID = 0;      
                    GetWindowThreadProcessId(hwndFocused, &dwProcessID);
                    processPid = dwProcessID;
                }               
                AttachThreadInput(GetCurrentThreadId(), idActive, FALSE);       
            }           
        }

最佳答案

您需要了解问题本身。由于安全风险以及可能有许多桌面(许多登录用户),服务无法与桌面交互。 GetForegroundWindow() 是与桌面的交互。没有简单的解决方法。复杂的涉及单独的进程作为 Windows 应用程序(不是服务)运行并与您的服务通信。

关于c++ - GetForegroundWindow 从 Windows 7 上的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582709/

相关文章:

c++ - 如何在 C++ 中创建和使用非类型模板以及将在何处使用

C++ malloc - 动态数组

c++ - 使用概念启用类模板的成员函数

c++ - Axter EzLogger - 如何删除警告或错误消息? i686-apple-darwin10-g++-4.2.1 (海湾合作委员会) 4.2.1

c++ - 此指针类型转换符号的含义

java - 我无法删除 Java 中的文件 (Windows 7)

python - 安装 SimpleCV 时遇到问题

.net - 确定 Windows 当前是否正在播放声音

windows - 如何正确地在任务栏中显示无模式表单

python - 如何使用管理员在 Python 中运行 cmd 命令