【发布时间】:2012-01-03 05:16:26
【问题描述】:
如何在 NSIS 页面中判断给定端口在本地或远程系统上是否可用?
【问题讨论】:
-
COM 端口?绑定到 IP 套接字?
如何在 NSIS 页面中判断给定端口在本地或远程系统上是否可用?
【问题讨论】:
关于 COM 端口:
它们不能直接从 NSIS 检测到,所以用 C 编写一个简单的插件,它将通过端口号检测端口。
这是我的基本想法:
void GetListOfLocalPorts(CList<CString, CString>& o_lstPorts)
{
for( int i = 1; i <= 99; i++ )
{
DCB dcb;
HANDLE hCom = NULL;
BYTE byPort = (BYTE)i;
CString strPort;
strPort.Format("COM%d", i);
CString strCom = (CString)"\\\\.\\" + strPort;
SetErrorMode(SEM_FAILCRITICALERRORS);
try
{
hCom = CreateFile(strCom, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hCom == INVALID_HANDLE_VALUE)
continue;
BOOL fSuccess = GetCommState(hCom, &dcb);
CloseHandle(hCom);
if (!fSuccess)
continue;
// Port exists on this machine
o_lstPorts.AddTail(strPort);
}
catch(...)
{
}
}
}
【讨论】: