unit Utility;

interface

uses
  System.Classes, Winapi.Windows, Winapi.ShellAPI, Vcl.Forms, System.SysUtils,
  Winapi.WinInet;

function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PDWORD): Longword;

function CheckUrl(Url: string): boolean;

procedure SendKey(Wnd, VK: Cardinal; Ctrl, Alt, Shift: Boolean);


implementation

function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PDWORD): Longword;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(StartupInfo, SizeOf(StartupInfo), #0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
  StartupInfo.wShowWindow := ShowCmd;
  if not CreateProcess(nil, @FileName[1], nil, nil, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
    Result := WAIT_FAILED
  else
  begin
    if wait = FALSE then
    begin
      if ProcID <> nil then
        ProcID^ := ProcessInfo.dwProcessId;
      result := WAIT_FAILED;
      exit;
    end;
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
  end;
  if ProcessInfo.hProcess <> 0 then
    CloseHandle(ProcessInfo.hProcess);
  if ProcessInfo.hThread <> 0 then
    CloseHandle(ProcessInfo.hThread);
end;

function CheckUrl(Url: string): boolean;
var
  hSession, hfile, hRequest: hInternet;
  dwindex, dwcodelen: dword;
  dwcode: array[1..20] of char;
  res: pchar;
begin
  if pos('http://', lowercase(Url)) = 0 then
    Url := 'http://' + Url;
  Result := false;
  hSession := InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if assigned(hSession) then
  begin
    hfile := InternetOpenUrl(hSession, pchar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
    dwindex := 0;
    dwcodelen := 10;
    HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodelen, dwindex);
    res := pchar(@dwcode);
    result := (res = '200') or (res = '302');
    if assigned(hfile) then
      InternetCloseHandle(hfile);
    InternetCloseHandle(hSession);
  end;

end;

procedure SendKey(Wnd, VK: Cardinal; Ctrl, Alt, Shift: Boolean);
var
  MC, MA, MS: Boolean;
begin
  // Try to bring target window to foreground
  ShowWindow(Wnd, SW_SHOW);
  SetForegroundWindow(Wnd);

  // Get current state of modifier keys
  MC := Hi(GetAsyncKeyState(VK_CONTROL)) > 127;
  MA := Hi(GetAsyncKeyState(VK_MENU)) > 127;
  MS := Hi(GetAsyncKeyState(VK_SHIFT)) > 127;

  // Press modifier keys if necessary (unless already pressed by real user)
  if Ctrl <> MC then
    keybd_event(VK_CONTROL, 0, Byte(MC) * KEYEVENTF_KEYUP, 0);
  if Alt <> MA then
    keybd_event(VK_MENU, 0, Byte(MA) * KEYEVENTF_KEYUP, 0);
  if Shift <> MS then
    keybd_event(VK_SHIFT, 0, Byte(MS) * KEYEVENTF_KEYUP, 0);

  // Press key
  keybd_event(VK, 0, 0, 0);
  keybd_event(VK, 0, KEYEVENTF_KEYUP, 0);

  // Release modifier keys if necessary
  if Ctrl <> MC then
    keybd_event(VK_CONTROL, 0, Byte(Ctrl) * KEYEVENTF_KEYUP, 0);
  if Alt <> MA then
    keybd_event(VK_MENU, 0, Byte(Alt) * KEYEVENTF_KEYUP, 0);
  if Shift <> MS then
    keybd_event(VK_SHIFT, 0, Byte(Shift) * KEYEVENTF_KEYUP, 0);
end;

end.

