Port availability check on remote system

*爱你&永不变心* 提交于 2020-01-07 02:11:32

问题


How can I tell if a given port is available or not on a local or remote system, in an NSIS page?


回答1:


Abuot COM ports:

They cannot be detected directly from NSIS so write simple plug-in in C which will detect port by it's number.

This is my basic idea:

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(...)
    {
    }
}
}


来源:https://stackoverflow.com/questions/8708226/port-availability-check-on-remote-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!