Windows屏幕捕获GDI和& DirectX失败返回ACCESS_DENIED

时间:2017-12-03 07:46:54

标签: windows visual-c++ directx screen-capture desktop-duplication

我使用DirectX和GDI分别在Windows上捕获屏幕,这些屏幕分别基于操作系统(DirectX(桌面复制) - 适用于Win7及以上版本和G7 for Win7及以下版本,或者在DirectX方法出现任何错误时作为后备)

在某些机器上我很少得到GDI和DirectX在winlogon屏幕上返回ACCESS_DENIED错误。

正在检测并正确设置桌面开关&应用程序以提升的用户权限运行,桌面设置期间没有错误。捕获屏幕的同一个线程正在调用将当前桌面设置为当前线程的函数。该应用程序未在任何RDP会话上运行,例如here&提到的问题。 here

我做了很多分析和研究。我确实浏览了开源屏幕捕获应用程序的源代码,但我没有找到任何有用的东西。我已经花了一个星期没有在这个问题上取得任何进展。

以下是设置当前桌面的代码

if (GetCurrentDesktop(aCurrentDesktop) && _tcscmp(aCurrentDesktop, aPrevDesktop) != 0)
{
    ScreenCaptureUtils::SetProcessWindowStation();

        HDESK hcurrentInputDesktop = OpenInputDesktop(0, FALSE, GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);

        if(hcurrentInputDesktop != NULL)
        {
            if(!SetThreadDesktop(hcurrentInputDesktop))
            {
                errorCode = GetLastError();
                LOG << _T("setCurrentInputDesktop: SetThreadDesktop failed. Error code: ") << errorCode;
            }
            else
            {
                errorCode = ERROR_SUCCESS;

                _tcscpy_s (aPrevDesktop, MAX_PATH, aCurrentDesktop);

                LOG << _T("setCurrentInputDesktop: SetThreadDesktop succeeded.");
            }

            hPrevDesk = hcurrentInputDesktop;

            CloseDesktop(hcurrentInputDesktop);
        }
        else
        {
            errorCode = GetLastError();
            LOG << _T("setCurrentInputDesktop: OpenInputDesktop failed. Error code: ") << errorCode;
        }
}
else
{
    LOG << _T("setCurrentInputDesktop: OpenInputDesktop failed. Error code: ") << errorCode;
}

bool GetCurrentDesktop ( TCHAR * desktopName )  
{
    {
        HDESK  hDesktop = ::OpenInputDesktop(0, FALSE, GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);

        if (!hDesktop)
        {
            return  false;
        }

        DWORD dw = 0;
        TCHAR  szName[MAX_PATH];
        ::memset(szName, 0, sizeof(szName));

        if (!::GetUserObjectInformation(hDesktop, UOI_NAME, &szName, sizeof(szName), &dw))
        {
            ::CloseDesktop(hDesktop);

            return false;
        }

        _tcscpy(desktopName, szName);

        ::CloseDesktop(hDesktop);
    }

}

捕获屏幕的代码类似于以下代码

int screenCapture()
{
    int result = -1;

    int width = 1000;
    int height = 700;

    HDC hdcTemp, hdc;
    BYTE* bitPointer;

    hdc = GetDC(HWND_DESKTOP);
    if (hdc != NULL)
    {
        hdcTemp = CreateCompatibleDC(hdc);
        if (hdcTemp != NULL)
        {
            BITMAPINFO bitmap;
            bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
            bitmap.bmiHeader.biWidth = width;
            bitmap.bmiHeader.biHeight = -height;
            bitmap.bmiHeader.biPlanes = 1;
            bitmap.bmiHeader.biBitCount = 24;
            bitmap.bmiHeader.biCompression = BI_RGB;
            bitmap.bmiHeader.biSizeImage = 0;
            bitmap.bmiHeader.biClrUsed = 0;
            bitmap.bmiHeader.biClrImportant = 0;

            HBITMAP hBitmap = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)&bitPointer, NULL, NULL);
            if (hBitmap != NULL)
            {
                HBITMAP hPrevBitmap = SelectObject(hdcTemp, hBitmap);

                BitBlt(hdcTemp, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
                result = (int) bitPointer[0];

                SelectObject(hdcTemp, hPrevBitmap);
                DeleteObject(hBitmap);
            }

            DeleteDC(hdcTemp);
       }

       ReleaseDC(HWND_DESKTOP, hdc);
    }

    return result;
}

我用当前监视器的名称调用GetDC。这是否与调用GetDC以NULL为参数有关?但根据this,这看起来并不像是一个问题。

任何帮助将不胜感激。提前谢谢。

0 个答案:

没有答案