13

I am working with screen sharing project.I am capturing desktop screen using below function.it works fine. But whenever secure desktop prompting for elevation.it returns black/empty image.

But when i turn off secured desktop from local security policy.It works fine.

Is there any way to capture secure desktop without disabling Local Security Policy.

static Bitmap CaptureDesktop()
{
    SIZE size;
    Bitmap printscreen = null;

    size.cx = Win32Stuff.GetSystemMetrics
                     (Win32Stuff.SM_CXSCREEN);

    size.cy = Win32Stuff.GetSystemMetrics
              (Win32Stuff.SM_CYSCREEN);

    int width = size.cx; int height = size.cy;

    IntPtr hWnd = Win32Stuff.GetDesktopWindow();
    IntPtr hDC = Win32Stuff.GetDC(hWnd);
    if (hDC != IntPtr.Zero)
    {
        IntPtr hMemDC = GDIStuff.CreateCompatibleDC(hDC);
        if (hMemDC != IntPtr.Zero)
        {
            IntPtr m_HBitmap = GDIStuff.CreateCompatibleBitmap(hDC, width, height);
            if (m_HBitmap != IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)GDIStuff.SelectObject(hMemDC, m_HBitmap);
                GDIStuff.BitBlt(hMemDC, 0, 0, width, height, hDC, 0, 0, GDIStuff.SRCCOPY);
                GDIStuff.SelectObject(hMemDC, hOld);
                GDIStuff.DeleteDC(hMemDC);
                printscreen = System.Drawing.Image.FromHbitmap(m_HBitmap);
                GDIStuff.DeleteObject(m_HBitmap);
            }
        }
    }
    Win32Stuff.ReleaseDC(hWnd, hDC);

    return printscreen;
}

Edit:

  1. Exe Installed in Secured location
  2. Exe is digitally signed
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Azar Shaikh
  • 445
  • 9
  • 26
  • This is not a solution but recommendation. I tried the same way for capturing the screen with the input audio and create a mpeg file. First problem is this way capturing the screen is a bit slow and consuming too much cpu. Second, there are limitations what can be grabbed from the video memory this way. That time, I searched the internet about screen capturing and found that successful applications hook the drawing APIs of Windows for this purpose. Checking the screen/window capture plugin of OBS may help. https://github.com/obsproject/obs-studio/tree/master/plugins/win-capture – NthDeveloper Sep 29 '18 at 07:58
  • Thanks @NthDeveloper i will check that – Azar Shaikh Sep 29 '18 at 09:18
  • I hope this link might help : https://www.c-sharpcorner.com/article/capture-desktop-activities-as-a-movie/ – xoxo Oct 04 '18 at 05:10
  • @KamalaHB thanks for link.it will not work out for me as its video recording. – Azar Shaikh Oct 04 '18 at 06:03

1 Answers1

7

In order to get the screen contents of the Secure Desktop, your application needs to fulfill some special criteria:

  • it must run under the SYSTEM account, not the logged-on user account
  • it must run on the Winlogon desktop, not on the user desktop
  • it should run as a service

To test it, you could e.g. use the SysInternals PsExec tool to run your application in that mode:

PsExec /h /x /d /s "path_to\your_application.exe"

The /x and /s switches are important: they run the process under the SYSTEM account and on the Winlogon desktop.

If you want to avoid using third-party tools, you need to create your own Windows service which will perform the screen captures of the Secure Desktop.

There is no source code of PsExec available, but you can look at the PAExec tool's source code - it's an open source alternative.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • I tried but very difficult to implement.I wonder how team viewer do this all without installing software in Machine. – Azar Shaikh Oct 09 '18 at 02:28
  • You don't need to install anything. You can directly run your service just as `PsExec` does. There is no source code of `PsExec` available, but you can look at the [`PAExec` tool source code](https://github.com/poweradminllc/PAExec) - it's an open source alternative. – dymanoid Oct 09 '18 at 08:35
  • Thanks it help me alot – Azar Shaikh Oct 09 '18 at 08:37
  • pls check this https://stackoverflow.com/questions/53101802/c-sharp-screenshot-winlogon-as-well-as-user-desktop. if you have any idea ? – Azar Shaikh Nov 01 '18 at 13:02