NSIS检测端口是否被占用

!include LogicLib.nsh

!define AF_INET         2
!define SOCK_STREAM     1
!define IPPROTO_TCP     6

Section
;Initialize Winsock
System::Alloc 400
Pop $0
System::Call Ws2_32::WSAStartup(i514,ir0)i.r1
StrCmp $1 0 0 done

;Create a SOCKET for listening for incoming connection requests
System::Call Ws2_32::socket(i${AF_INET},i${SOCK_STREAM},i${IPPROTO_TCP})i.r1
StrCmp $1 Cleanup
System::Call Ws2_32::inet_addr(t"127.0.0.1")i.s
System::Call Ws2_32::htons(i80)i.s
System::Call "*(&i2 ${AF_INET}, &i2 s, i s, &t8) i.r2"
;Bind the socket.
System::Call Ws2_32::bind(ir1,ir2,i16)i.r3
${If} $3 = -1
MessageBox MB_ICONINFORMATION|MB_OK "bind() failed."
System::Call Ws2_32::WSAGetLastError()i.r3
  ${If} $3 10048
    MessageBox MB_ICONINFORMATION|MB_OK "80端口被占用"
  ${EndIf}
${EndIf}
System::Call Ws2_32::closesocket(ir1)
System::Free $2
Cleanup:
System::Call Ws2_32::WSACleanup()
done:
System::Free $0
SectionEnd

原文地址:https://www.cnblogs.com/juin/p/2592071.html