title: How can I capture the screen from a windows service?
body: <p>I am writing a remote desktop application and I want to take a screenshot of the log in screen, the current logged user desktop, the screensaver and the UAC screen.</p>
  
  <p>As a desktop application, taking a screenshot of the user desktop works fine.</p>
  
  <p>Now I am implementing a windows service so that I can be able to take a screenshot of the UAC screen, the log in screen and the screensaver. However whatever I tried so far results in a black image.</p>
  
  <p>I am using QService for writting a cross platform windows service and it starts fine. This is where I have reached until now to take a screenshot from the windows service:</p>
  
  <p>On the main function I open the WinSta0 window station and create a new desktop</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include "andamaservice.h"
  
  int main(int argc, char *argv[])
  {
  
      auto winsta = OpenWindowStation(L"WinSta0", true, GENERIC_ALL);
          qDebug("OpenWindowStation lasterror =%u", GetLastError());
  
  
          //SECURITY_ATTRIBUTES attributes = {sizeof(SECURITY_ATTRIBUTES), 0, true};
          //attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
          //attributes.bInheritHandle = true;
          //hNewDesktop = CreateDesktop("NewDesktopName", NULL, NULL, 0 , GENERIC_ALL,  &amp;stSecurityAttr);
  
      auto hwinsta2 = SetProcessWindowStation(winsta);
          qDebug("SetProcessWindowStation lasterror =%u", GetLastError());
  
  
      //auto desktop = OpenDesktop(L"default", 0, false, GENERIC_ALL);
      //qDebug("OpenDesktop lasterror = %u", GetLastError());
  
  
      // Create the desktop.
      auto desktop = CreateDesktop(L"newdesktop",
                                 NULL,
                                 NULL,
                                 0,
                                 GENERIC_ALL,
                                 &amp;attributes);
      qDebug("CreateDesktop lasterror =%u", GetLastError());
  
  
      SetThreadDesktop(desktop);
      //SwitchDesktop(desktop);
      qDebug("SetThreadDesktop lasterror =%u", GetLastError());
  
  
      AndamaService service(argc, argv);
      return service.exec();
  }
  </code></pre>
  
  <p>then, at some other point in my code, I am taking the screenshot, but the result is a black image. This is the code I am using:</p>
  
  <pre><code>// get the device context of the screen
  HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  // and a device context to put it in
  HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
  
  int x = GetDeviceCaps(hScreenDC, HORZRES);
  int y = GetDeviceCaps(hScreenDC, VERTRES);
  
  // maybe worth checking these are positive values
  HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
  
  // get a new bitmap
  HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
  
  BitBlt(hMemoryDC, 0, 0, x, y, hScreenDC, 0, 0, SRCCOPY);
  hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
  
  qimg = QtWin::fromHBITMAP(hBitmap, QtWin::HBitmapNoAlpha).toImage();
  
  // clean up
  DeleteDC(hMemoryDC);
  DeleteDC(hScreenDC);
  </code></pre>
  
  <p>The complete application source code and executables are here: <a href="http://andama.org/source-code" rel="nofollow">http://andama.org/source-code</a></p>
  
  <p>How can I correctly get a screenshot of the desktop (or log in screen, or UAC screen) from a windows service?</p>