Guest User

Snippit covering showing Rdp Remote App Window

a guest
Jul 20th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.01 KB | None | 0 0
  1. // Some example code that may help you with activating an Rdp remote application
  2. // Possibly incomplete as this is taken from a complete application but it hopefully will
  3. // help. If you have any improvements to suggest, email [email protected]
  4.  
  5. // No copyright in the below code is claimed (though it is an original work based upon code from a number
  6. // of different sources).
  7.  
  8. function FindRdpWindowHandleCallback(handle: THandle; lParam: LPARAM): Boolean; stdcall;
  9.  
  10.   function isMainWindow(handle: HWND): Boolean;
  11.   begin
  12.     result := (GetWindow(handle, GW_OWNER) = HWND(0)) and (IsWindowVisible(handle) or IsIconic(handle));
  13.   end;
  14.  
  15. var
  16.   handleData: PHandleData;
  17.   processId: ULONG;
  18.   hwClassName: Array[0..255] of Char;
  19. begin
  20.   if GetClassName(handle, hwClassName, SizeOf(hwClassName)) <> 0 then begin
  21.     // TXxxYyyZzz is the class of a form created by the DLL, which manages communication
  22.     // via the virtual channels.
  23.     if (StrComp(hwClassName, 'TXxxYyyZzz') = 0) then
  24.       // If we have an exact class name match, we have our window
  25.       result := true
  26.     else begin
  27.       handleData := PHandleData(lParam);
  28.       processId := 0;
  29.       // If no exact match, try comparing process ids. If we have a match,
  30.       // it is possibly the window we are looking for.
  31.       GetWindowThreadProcessId(handle, &processId);
  32.       if (handleData^.processId = processId) and isMainWindow(handle) then begin
  33.         handleData^.bestHandle := handle;
  34.         result := false;
  35.       end else
  36.         result := true
  37.     end;
  38.   end else
  39.     result := true;
  40. end;
  41.  
  42. function ForceForegroundWindow(lnHWND: HWND): Boolean;
  43. var
  44.   nForeThread: Cardinal;
  45.   nAppThread: Cardinal;
  46. begin
  47.   nForeThread := GetWindowThreadProcessId(GetForegroundWindow(), NIL);
  48.   nAppThread := GetCurrentThreadId();
  49.   if nForeThread <> nAppThread then begin
  50.     if AttachThreadInput(nForeThread, nAppThread, True) then begin
  51.       if BringWindowToTop(lnHWND) then
  52.         result := ShowWindow(lnHWND, 0)
  53.       else
  54.         result := False;
  55.       AttachThreadInput(nForeThread, nAppThread, False);
  56.     end else
  57.       result := False;
  58.   end else begin
  59.     if BringWindowToTop(lnHWND) then
  60.       result := ShowWindow(lnHWND, 0)
  61.     else
  62.       result := False;
  63.   end;
  64. end;
  65.  
  66. function TRdpChannel.GetRdpHandle: HWND;
  67. begin
  68.   // Usually we have the Rdp window handle becaause we create the process
  69.   if IsWindow(FRdpHandle) then
  70.     result := FRdpHandle
  71.   else begin
  72.     // If the locally maintained Rdp handle no longer refers to a window, attempt to
  73.     // locate an open RDP manager window (created by the DLL)
  74.     GetWindowThreadProcessId(FCallManager.Handle, FHandleData.processId);
  75.     FHandleData.bestHandle := 0;
  76.     EnumWindows(@FindRdpWindowHandleCallback, NativeInt(@FHandleData));
  77.     FRdpHandle := FHandleData.bestHandle;
  78.     result := FRdpHandle;
  79.   end;
  80. end;
  81.  
  82. procedure TRdpChannel.ShowApplication;
  83. var
  84.   h: HWND;
  85.   hClassName: Array[0..255] of Char;
  86. begin
  87.   h := GetRdpHandle;
  88.   SetStatus(5, 'Show application called');
  89.   if IsWindow(h) then begin
  90.     if GetClassName(h, hClassName, SizeOf(hClassName)) <> 0 then begin
  91.       // Attempts to restore a Delphi app, which has a 0 pixel main window
  92.       // and apparently documentation says that it should be restored by
  93.       // sending a WM_SYSCOMMAND message with SC_RESTORE as a parameter. Does
  94.       // not always appear to work if the user clicks the minimise button on the
  95.       // app main window.
  96.       if (StrComp(hClassName, 'TApplication') = 0) then
  97.         PostMessage(h, WM_SYSCOMMAND, SC_RESTORE, 0);
  98.     end;
  99.     if ShowWindow(h, SW_RESTORE) then begin
  100.       if not SetForegroundWindow(h) then
  101.         ForceForegroundWindow(h);
  102.     end if ShowWindow(h, SW_SHOWDEFAULT) then begin
  103.       if not SetForegroundWindow(h) then
  104.         ForceForegroundWindow(h);
  105.     end else begin
  106.       if ShowWindow(h, SW_MINIMIZE) then begin
  107.         if not OpenIcon(h) then
  108.           ForceForegroundWindow(h);
  109.       end else
  110.         ForceForegroundWindow(h);
  111.     end;
  112.   end;
  113. end;
Advertisement
Add Comment
Please, Sign In to add comment