//#############################IsNotPortOccupation函数###################################
// 自定义函数,判断端口占用
function IsNotPortOccupation(strPortNum: String): Boolean;
// 变量定义
var ErrorCode: Integer;
var bRes: Boolean;
var strFileContent: AnsiString;
var strTmpPath: String; // 临时目录
var strTmpFile: String; // 临时文件,保存查找软件数据结果
var strCmdFind: String; // 查找端口命令, netstat -natp tcp |findstr "LISTENING" |findstr "135"| find /C "135" 找到的话返回个数,找不到为0
begin
strTmpPath := GetTempDir();
strTmpFile := Format('%sfindProtRes.txt', [strTmpPath]);
strCmdFind := Format('/c netstat -natp tcp |findstr "LISTENING" |findstr ":%s"|find /C ":%s" > "%s"', [strPortNum, strPortNum ,strTmpFile]);
log(strCmdFind);
bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdFind, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
bRes := LoadStringFromFile(strTmpFile, strFileContent);
strFileContent := Trim(strFileContent);
if StrToInt(strFileContent) > 0 then begin
result:=true;
end else result:=false;
end;
//#############################IsNotProcessRun函数###################################
procedure InitializeWizard();
begin
if IsNotPortOccupation('80') then begin
MsgBox('80端口占用', mbInformation, MB_OK);
end else
MsgBox('80端口没有占用', mbInformation, MB_OK);
end;
延伸,判断 windows 系统有没有安装指定服务的方法类似。代码如下:
//#############################IsNotInstalledServices函数###################################
// 自定义函数,查看系统有没有安装的服务
function IsNotInstalledServices(strServiceName: String): Boolean;
// 变量定义
var ErrorCode: Integer;
var bRes: Boolean;
var strFileContent: AnsiString;
var strTmpPath: String; // 临时目录
var strTmpFile: String; // 临时文件,保存查找软件数据结果
var strCmdFind: String; // 查找端口命令
begin
strTmpPath := GetTempDir();
strTmpFile := Format('%sfindServicesRes.txt', [strTmpPath]);
strCmdFind := Format('/c sc query state= all | findstr "%s"| findstr "SERVICE_NAME" | find /C "%s" > "%s"', [strServiceName, strServiceName ,strTmpFile]);
log(strCmdFind);
bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdFind, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
bRes := LoadStringFromFile(strTmpFile, strFileContent);
strFileContent := Trim(strFileContent);
if StrToInt(strFileContent) > 0 then begin
result:=true;
end else result:=false;
end;
procedure InitializeWizard();
begin
if IsNotInstalledServices('Apache2.4') then begin
MsgBox('Apache2.4服务已安装', mbInformation, MB_OK);
end else
MsgBox('Apache2.4服务未安装', mbInformation, MB_OK);
end;
//#############################IsNotInstalledServices函数###################################