screenshot can't be captured when winlogon screen is displayed

34 views Asked by At

I want to capture screenshots of the screen via a user process which is coded in C++. The approach successfully captures the screen while a user logs in an account but when winlogon screen is displayed, screenshots became all black and BitBlt function fails and gives Error Code 6. How can I successfully capture winlogon screen, too?

void captureAndSave()
{
    HDC screenDC = GetDC(0); // Get the device context of the entire screen
    if (!screenDC) {
        qWarning("Failed to get screen device context.");
        return;
    }

    int screenWidth = GetDeviceCaps(screenDC, HORZRES);
    int screenHeight = GetDeviceCaps(screenDC, VERTRES);

    HDC memDC = CreateCompatibleDC(screenDC);
    if (!memDC) {
        ReleaseDC(0, screenDC);
        qWarning("Failed to create compatible device context.");
        return;
    }

    HBITMAP hBitmap = CreateCompatibleBitmap(screenDC, screenWidth, screenHeight);
    if (!hBitmap) {
        DeleteDC(memDC);
        ReleaseDC(0, screenDC);
        qWarning("Failed to create compatible bitmap.");
        return;
    }

    SelectObject(memDC, hBitmap);
    if (!BitBlt(memDC, 0, 0, screenWidth, screenHeight, screenDC, 0, 0, SRCCOPY))
    {
        qWarning("Failed to BitBlt. Error code: %d", GetLastError());
    }

    QString fileName = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss") + ".bmp";

    if (!filePath.isEmpty()) {
        SaveBitmapToFile(hBitmap, filePath.toStdWString().c_str());
        qDebug("Screenshot saved to: %s", qUtf8Printable(filePath));
    }

    DeleteObject(hBitmap);
    DeleteDC(memDC);
    ReleaseDC(0, screenDC);
}

I tried to implement How to switch a process between default desktop and Winlogon desktop? this approach but it gives "OpenDesktop: Error Code: 5 Access is denied." error and doesn't work neither.

Thank you...

0

There are 0 answers