How to check if a port can be used in Inno Setup?

Do I need to check that some port can be used or not? How can this be done in Inno Setup? Is there a way to use a socket in Inno Setup? Is there a library for this? If there is a way to import and use it?

Thank you for your responses.

+3
source share
2 answers

You can use my function to check if the port is accessible:

function CheckPortOccupied(Port:String):Boolean;
var
  ResultCode: Integer;
begin
  Exec(ExpandConstant('{cmd}'), '/C netstat -na | findstr'+' /C:":'+Port+' "', '', 0,
       ewWaitUntilTerminated, ResultCode);
  if ResultCode <> 1 then 
  begin
    Log('this port('+Port+') is occupied');
    Result := True; 
  end
    else
  begin
    Result := False;
  end;
end;
+1
source

The return function (in MsgBox) of a service or program that uses port 80. MsgBox will not be displayed if the output is empty.

function NextButtonClick(CurPage: Integer): Boolean;
var
  TmpFileName, ExecStdout: string;
  ResultCode: integer;
begin
  if CurPage = wpWelcome then
  begin
    TmpFileName := ExpandConstant('{tmp}') + '\~pid.txt';
    Exec('cmd.exe',
         '/C FOR /F "usebackq tokens=5 delims= " %i IN (`netstat -ano ^|find "0.0:80"`) DO '
           + '@tasklist /fi "pid eq %i" | find "%i" > "' + TmpFileName + '"', '', SW_HIDE,
         ewWaitUntilTerminated, ResultCode);
    if LoadStringFromFile(TmpFileName, ExecStdout) then
    begin
      MsgBox('Port 80 is used by '#13 + ExecStdout, mbInformation, MB_OK);
    end;
    DeleteFile(TmpFileName);
  end;
  Result := True;
end;        
0
source

All Articles