【问题标题】:How to check if port is usable in Inno Setup?如何检查端口是否在 Inno Setup 中可用?
【发布时间】:2014-02-11 12:27:07
【问题描述】:

我需要检查一些端口是否可用?如何在 Inno Setup 中做到这一点? 有没有办法在 Inno Setup 中使用套接字?有没有这方面的图书馆?如果有怎么导入使用?

感谢您的回答。

【问题讨论】:

标签: inno-setup pascalscript


【解决方案1】:

您可以使用我的功能来检查端口是否可用:

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;

【讨论】:

  • 尽可能避免使用命令行工具。始终更喜欢本机 Windows API 解决方案。在这种情况下,您可以例如尝试绑定到给定端口的套接字并查看它是否失败。
  • 确实如此。我在 4 个月前解决了问题。你的答案是正确的。
【解决方案2】:

返回(在 MsgBox 中)使用端口 80 的服务或程序的函数。 如果输出为空,则不会显示 MsgBox。

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;        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    相关资源
    最近更新 更多