Exemplo n.º 1
0
BOOL ListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue)
{
  OSVERSIONINFO osvinfo;

  /* check parameters */

  if(!lpCbk){
    SetLastError(ERROR_INVALID_PARAMETER);
    return FALSE;
  }

  /* determine which platform we're running on and forward
   * to the appropriate routine
   */

  ZeroMemory(&osvinfo,sizeof(osvinfo));
  osvinfo.dwOSVersionInfoSize=sizeof(osvinfo);

  GetVersionEx(&osvinfo);

  switch(osvinfo.dwPlatformId){
    case VER_PLATFORM_WIN32_WINDOWS:
      return Win9xListPorts(lpCbk,lpCbkValue);
      break;
    case VER_PLATFORM_WIN32_NT:
      return WinNT40ListPorts(lpCbk,lpCbkValue);
      break;
    default:
      SetLastError(ERROR_OLD_WIN_VERSION);
      return FALSE;
      break;
  }
}
Exemplo n.º 2
0
void CEnumSerial::EnumSerialPorts(CArray<SSerInfo,SSerInfo&> &asi, BOOL bIgnoreBusyPorts)
{
    // Clear the output array
    asi.RemoveAll();

    // Use different techniques to enumerate the available serial
    // ports, depending on the OS we're using
    OSVERSIONINFO vi;
    vi.dwOSVersionInfoSize = sizeof(vi);
    if (!GetVersionEx(&vi))
    {
        CString str;
        str.Format(_T("Could not get OS version. (err=%lx)"), GetLastError());
        throw str;
    }

    // Handle windows 9x and NT4 specially
    if (vi.dwMajorVersion < 5)
    {
        if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT)
            EnumPortsWNt4(asi);
        else
            Win9xListPorts(asi);
    }
    else
    {
        // Win2k and later support a standard API for
        // enumerating hardware devices.
        EnumPortsWdm(asi);
    }

    for (int ii=0; ii<asi.GetSize(); ii++)
    {
        SSerInfo& rsi = asi[ii];
        if (bIgnoreBusyPorts)
        {
            // Only display ports that can be opened for read/write
            HANDLE hCom;
            hCom = CreateFile(rsi.strDevPath, GENERIC_READ | GENERIC_WRITE, 0,  NULL,
                              OPEN_EXISTING, 0, NULL);

            if (hCom == INVALID_HANDLE_VALUE)
            {
                // It can't be opened; remove it.
                asi.RemoveAt(ii);
                ii--;
                continue;
            }
            else
            {
                // It can be opened! Close it and add it to the list
                CloseHandle(hCom);
            }
        }

        // Come up with a name for the device.
        // If there is no friendly name, use the port name.
        if (rsi.strFriendlyName.IsEmpty())
            rsi.strFriendlyName = rsi.strPortName;

        // If there is no description, try to make one up from
        // the friendly name.
        if (rsi.strPortDesc.IsEmpty())
        {
            // If the port name is of the form "ACME Port (COM3)"
            // then strip off the " (COM3)"
            rsi.strPortDesc = rsi.strFriendlyName;
            int startdex = rsi.strPortDesc.Find(_T(" ("));
            int enddex = rsi.strPortDesc.Find(_T(")"));
            if ( (startdex > 0) && (enddex == (rsi.strPortDesc.GetLength()-1)))
                rsi.strPortDesc = rsi.strPortDesc.Left(startdex);
        }
    }
}