Search code examples
delphiscreenshot

How capture a window that is opened inside of a “new desktop environment”?


Good afternoon to all.

I want see a window that is open inside of a "new desktop environment", from of my remote assistance tool, but I'm not able to see this window using conventional functions like this below:

    function RandomPassword(PLen: Integer): string;
    var
      str: string;
    begin
      Randomize;
      str    := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      Result := '';
      repeat
        Result := Result + str[Random(Length(str)) + 1];
      until (Length(Result) = PLen)
    end;

        procedure Printscreen;
        var
          DCDesk: HDC;
          bmp: TBitmap;
          hmod, hmod2 : HMODULE;
          BitBltAPI: function(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD): BOOL; stdcall;
          GetWindowDCAPI: function(hWnd: HWND): HDC; stdcall;

          begin

         hmod := GetModuleHandle('Gdi32.dll');
         hmod2:= GetModuleHandle('User32.dll');

         if (hmod <> 0) and (hmod2 <> 0) then begin

          bmp := TBitmap.Create;

          bmp.Height := Screen.Height;
          bmp.Width := Screen.Width;

          GetWindowDCAPI:= GetProcAddress(hmod2, 'GetWindowDC');

          if (@GetWindowDCAPI <> nil) then begin

            DCDesk := GetWindowDCAPI(GetDesktopWindow);

          end;

          BitBltAPI:= GetProcAddress(hmod, 'BitBlt');

          if (@BitBltAPI <> nil) then begin

            BitBltAPI(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height, DCDesk, 0, 0, SRCCOPY);

            bmp.SaveToFile('ScreenShot_------_' + RandomPassword(8) + '.bmp');

          end;

            ReleaseDC(GetDesktopWindow, DCDesk);

            bmp.Free;

            FreeLibrary(hmod);
            FreeLibrary(hmod2);

         end;
        end;

begin
   while True do
    begin
       Printscreen;
      Sleep(5000);
    end;

end.

That produces this result

Already using Team View software for example, the window appears on screen capture normally and produces this result.

So, exist some way for see this window in screen capture like is possible from of Team View software?

All suggestions will be welcomed.


Solution

  • It seems that you are trying to get the screenshot of a secure desktop. If that is the case first you must read the documentation about this topic. Because this is not a trivial task (you must know about Sessions, Desktops and Windows Stations). Also your application must be a trusted process running from the Local SYSTEM Account.

    From here now you must do this.

    Recommended Lecture