InstallShield CMD检测端口占用

IS版本及工程类型:InstallShield 2015,Basic MSI

function checkPort()
STRING szPortNumber, svLine, szCmdLine;
NUMBER nvFileHandle, nLocation;
begin   
    szPortNumber = "8080";
     //把8080端口的使用情况,以txt文档形式写入到系统临时文件夹里(FOLDER_TEMP是IS提供的系统临时文件夹,直接调用)
    szCmdLine = "netstat -an | find \"" + szPortNumber + "\" > \"" + FOLDER_TEMP ^ "stdout.txt\"";
    LaunchApplication( "cmd.exe", "/C " + szCmdLine, "", SW_HIDE, 20000, LAAW_OPTION_WAIT );
    OpenFileMode (FILE_MODE_NORMAL);
    if(OpenFile (nvFileHandle, FOLDER_TEMP, "stdout.txt") < 0) then
        MessageBox ("OpenFile failed to open " + FOLDER_TEMP ^ "stdout.txt", SEVERE);
        abort;
    endif;
    while GetLine (nvFileHandle, svLine) = 0    
        nLocation = StrFind(svLine, "LISTENING") + StrFind(svLine, "ESTABLISHED");
        if (nLocation > 0 ) then
            MessageBox("[Port:" + szPortNumber + "] is already occupied,please reinstall after resolving.", WARNING);
            return -1;
        endif;
    endwhile;
    CloseFile (nvFileHandle);
end;

注意,cmd.exe后面,一定要加"/c ",不加的话后续命令不执行。下面用两个简单的bat脚本尝试启动TIM.exe说明下。

1.【lauchTIMNotIncludeC.bat】

cmd start C:\Users\Jayce\"Program Files"\TIM\Bin\TIM.exe
pause

没有加"/c ":只执行到cmd,后续命令未执行,启动程序失败。

未启动TIM.exe程序.png

2.【lauchTIMIncludeC.bat.bat】

cmd /C start C:\Users\Jayce\"Program Files"\TIM\Bin\TIM.exe
pause

加"/c ": 成功启动TIM程序。


成功启动TIM.exe程序

你可能感兴趣的:(InstallShield CMD检测端口占用)