title: The desktop capture with DirectX does not work
body: <p>Because processing was slow by D3DPOOL_SCRATCH, I wrote the desktop capture program to reference for the report on the Internet. However, a result is a pitch-black picture. Is this the result of a console program or is there any other cause? </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;d3d9.h&gt;
  
  void main()
  {
      CoInitialize(NULL);
  
      LPDIRECT3D9 d3d9;
      LPDIRECT3DDEVICE9 d3ddev;
      d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  
      int ww = GetSystemMetrics(SM_CXSCREEN);
      int wh = GetSystemMetrics(SM_CYSCREEN);
  
      HWND hwnd = GetDesktopWindow();
      D3DPRESENT_PARAMETERS d3dpp;
      ZeroMemory(&amp;d3dpp, sizeof(d3dpp));
      d3dpp.Windowed = TRUE;
      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
      d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
      d3dpp.BackBufferCount = 1;
      d3dpp.BackBufferWidth = ww;
      d3dpp.BackBufferHeight = wh;
      d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
      d3dpp.MultiSampleQuality = 0;
      d3dpp.EnableAutoDepthStencil = TRUE;
      d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
      d3dpp.hDeviceWindow = hwnd;
      d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
      d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
      d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  
      d3d9-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &amp;d3dpp, &amp;d3ddev);
  
      IDirect3DSurface9* render;
      IDirect3DSurface9* dest;
      d3ddev-&gt;CreateOffscreenPlainSurface(ww, wh, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &amp;dest, NULL);
      d3ddev-&gt;GetRenderTarget(0, &amp;render);
      d3ddev-&gt;GetRenderTargetData(render, dest);
  
      D3DLOCKED_RECT bits;
      dest-&gt;LockRect(&amp;bits, NULL, D3DLOCK_READONLY);
  
      // If a capture is successful, colors other than black(0x00000000) should enter. 
      for(int i = 0; i &lt; 100; i++){
          printf("%02X ", *((BYTE*)bits.pBits + i));
      }
  
      dest-&gt;UnlockRect();
  
      render-&gt;Release();
      dest-&gt;Release();
      d3ddev-&gt;Release();
      d3d9-&gt;Release();
  
      CoUninitialize();
  }
  </code></pre>