【问题标题】:Winlogon screen capturing in Windows 7/10Windows 7/10 中的 Winlogon 屏幕截图
【发布时间】:2017-09-17 03:19:31
【问题描述】:

我需要在 WinXP/Win7/10 中捕获一个 winlogon 屏幕。 对于 WinXP,我使用的是镜像驱动程序和这样的标准方法:

...
extern "C" __declspec(dllexport) void SetActiveDesktop() {
    if ( currentDesk != NULL )
        CloseDesktop( currentDesk );

    currentDesk = OpenInputDesktop( 0, FALSE, GENERIC_ALL );
    BOOL ret = SetThreadDesktop( currentDesk );
    int LASTeRR = GetLastError();
}

extern "C" __declspec(dllexport) HBITMAP CaptureAnImage(
    int width, 
    int height,
    int bitsPerPixel )
{
    HBITMAP hbmScreen;
    LPTSTR bih = NULL;
    HDC hdcMemDC = NULL;

    int colorDepth = GetCurrentColorDepth();

    if ( bitsPerPixel > colorDepth && colorDepth > 0 )
        bitsPerPixel = colorDepth;

    // Checks a current HDC
    if ( currHdc == NULL ) {
        SetActiveDesktop();
        currHdc = GetDcMirror();
    }

    if ( prevHdc != currHdc ) {
        prevHdc = currHdc;
    }

    // Check an application instance handler
    if ( appInstance == NULL )
        appInstance = GetModuleHandle(NULL);

    // Creates a compatible DC which is used in a BitBlt from the window DC
    hdcMemDC = CreateCompatibleDC( currHdc ); 

    if( hdcMemDC == NULL )
    {
        return NULL;
    }

    // Defines bitmap parameters    
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = width;
    bi.bmiHeader.biHeight = height;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = bitsPerPixel;

    // Creates a bitmap with defined parameters 
    hbmScreen = CreateDIBSection( hdcMemDC, &bi, DIB_RGB_COLORS, (VOID**)&lpBitmapBits, NULL, 0 );

    if ( hbmScreen == NULL ) {
        return NULL;
    }

    // Select the compatible bitmap into the compatible memory DC.
    SelectObject(hdcMemDC,hbmScreen);

    // Bit block transfer into our compatible memory DC.
    if(!BitBlt(hdcMemDC, 
        0,0, 
        width, height,
        currHdc, 
        0,0,
        SRCCOPY))
    {
        SetActiveDesktop();
        currHdc = GetDC(NULL);//GetDcMirror();      
        hdcMemDC = CreateCompatibleDC( currHdc );

        // Creates a bitmap with defined parameters 
        hbmScreen = CreateDIBSection( hdcMemDC, &bi, DIB_RGB_COLORS, (VOID**)&lpBitmapBits, NULL, 0 );

        if(!BitBlt(hdcMemDC,
            0,0, 
            width, height,
            currHdc,
            0,0,
            SRCCOPY )) 
        {
            DeleteDC( hdcMemDC ); 
            return hbmScreen;
        }
    }

    if (DeleteDC( hdcMemDC ) == FALSE ) {
        return NULL;
    }

    return hbmScreen;
}

幸运的是它可以在 WinXP 上运行。但在 win7/win10 的情况下,我的情况就完全不同了:

切换到 winlogon 后的 SetThreadDesktop 函数总是返回 FALSE,错误为 5(拒绝访问) 我试图改变策略:

  • 首先,程序会创建一个包含所有现有窗口站及其桌面的列表。
  • 在该程序“轮询”所有 WINSTA 和 HDESK 并将屏幕截图保存在磁盘上之后。

    我尝试在 3 种模式下启动此程序:

    • 作为管理员
    • 作为启用桌面交互的服务。
    • 在 winlogon 桌面上使用 CreateProcess 标志(在这种情况下程序只是崩溃)

结果是一样的。 我做错了什么?我应该尝试使用 Desktop Duplication API 吗?

提前感谢您的回复!

【问题讨论】:

    标签: c++ windows-7 windows-10 display winlogon


    【解决方案1】:

    由于 Winlogon 是一个安全桌面,您必须在 LOCAL_SYSTEM 帐户下运行您的应用程序才能访问它。

    示例:在 LOCAL_SYSTEM 下运行的 Windows 服务在控制台会话中启动用户应用程序(捕获屏幕)。

    在您的代码中,没有检查 OpenInputDesktop 的返回值,该值可能为 NULL,错误代码为 5(访问被拒绝)。

    查看this answer 了解更多信息

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 2011-06-03
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多