在Windows 7/10中捕获Winlogon屏幕

时间:2017-04-20 13:03:44

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

我需要在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标志(在这种情况下程序只是崩溃)

结果是一样的。 我做错了什么?我应该尝试桌面复制API吗?

提前感谢您的回复!

1 个答案:

答案 0 :(得分:1)

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

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

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

查看this answer以获取更多信息