2012-04-26 124 views
2

我正在使用NSIS爲我的應用程序開發安裝程序。這個安裝程序需要做的是檢查端口80是否可用,如果可用,繼續安裝程序,如果它沒有給出端口80上運行的進程名稱的錯誤消息。如何檢查NSIS中特定端口上運行的進程

我找到了一種方法來檢查端口80是否可用。爲此,我使用Ports.nsh作爲插件。 http://nsis.sourceforge.net/Check_open_ports

${If} ${TCPPortOpen} 80 
MessageBox MB_OK|MB_ICONSTOP "PORT 80 is already using by another program..." 
Abort 
${EndIf} 

但是這個我無法找到該端口上正在運行的進程。我需要給出一個錯誤信息,如

//Skype is running on port 80 and close Skype to continue with the installation. 

有人可以幫助我這個。謝謝。

回答

1

由於我找不到在nsis中這樣做的正確方法,我只是使用VBScript來檢查進程名稱並通過nsis腳本調用它。 Follwing是代碼。

//TestPort80.vbs

Dim WshShell, oExec, key 
Set WshShell = CreateObject("WScript.Shell") 
Set oExec = WshShell.Exec("netstat -o -n -a") 
key = "0.0:80" 

Dim values 
values = checkPortStatus(oExec, key) 
portInUse = values(0) 
input = values(1) 

If portInUse Then 
    x = InStrRev(input, " ") 

    ProcessID = Mid(input, x+1) 

    commandTxt = "tasklist /FI " & Chr(34) & "PID eq " & ProcessID & Chr(34) 

Dim oExec2, key2 
Set oExec2 = WshShell.Exec(commandTxt) 
key2 = ProcessID 

Dim values2 
values2 = checkPortStatus(oExec2, key2) 
Found = values2(0) 
input2 = values2(1) 

If Found Then 
    y = InStr(input2, " ") 
    ExeName = Left(input2, y-1) 
     WScript.StdOut.WriteLine "Port 80 is using by " & ExeName 
End If 
End If 
'## If we explicitly set a Success code then we can avoid this. 
WScript.Quit 512 

Function checkPortStatus(oExec, key) 
portInUse = false 
input = "" 
Do While True 

    If Not oExec.StdOut.AtEndOfStream Then 
      input = oExec.StdOut.ReadLine() 
      If InStr(input, key) <> 0 Then 
     ' Found Port 80 
       portInUse = true 
       Exit DO 
      End If 
    Else 
     Exit DO 
    End If 
    WScript.Sleep 100 
Loop 

Do While oExec.Status <> 1 
WScript.Sleep 100 
Loop 
Dim values(1) 
values(0) = portInUse 
values(1) = input 

checkPortStatus = values 

End Function 

//Installer.nsi

${If} ${TCPPortOpen} 80 
GetTempFileName $0 
    File /oname=$0 `TestPort80.vbs` 
    nsExec::ExecToStack `"$SYSDIR\CScript.exe" $0 //e:vbscript //B //NOLOGO` 
Pop $0 
Pop $1 
MessageBox MB_OK|MB_ICONSTOP '$1' 
Abort 
${EndIf} 
相關問題