Пример #1
0
bool
PortEnumerator::Next()
{
  assert(!drivers_active.error());

  TCHAR key_name[64];
  while (drivers_active.enum_key(i++, key_name, 64)) {
    RegistryKey device(drivers_active, key_name, true);
    if (device.error())
      continue;

    TCHAR device_key[64];
    if (device.get_value(_T("Key"), device_key, 64) &&
        IsSerialPort(device_key) &&
        device.get_value(_T("Name"), name.buffer(), name.MAX_SIZE)) {
      display_name = name;
      const size_t length = display_name.length();
      TCHAR *const tail = display_name.buffer() + length;
      const size_t remaining = display_name.MAX_SIZE - length - 3;

      if (GetDeviceFriendlyName(device_key, tail + 2, remaining)) {
        /* build a string in the form: "COM1: (Friendly Name)" */
        tail[0] = _T(' ');
        tail[1] = _T('(');
        _tcscat(tail, _T(")"));
      }

      return true;
    }
  }

  return false;
}
Пример #2
0
bool
PortEnumerator::Next()
{
  assert(!drivers_active.error());

  TCHAR key_name[64];

  /* enumerate regular serial ports first */

  while (drivers_active.EnumKey(i++, key_name, 64)) {
    RegistryKey device(drivers_active, key_name, true);
    if (device.error())
      continue;

    TCHAR device_key[64];
    if (device.GetValue(_T("Key"), device_key, 64) &&
        IsSerialPort(device_key) &&
        device.GetValue(_T("Name"), name.buffer(), name.MAX_SIZE)) {
      display_name = name;
      const size_t length = display_name.length();
      TCHAR *const tail = display_name.buffer() + length;
      const size_t remaining = display_name.MAX_SIZE - length - 3;

      if (GetDeviceFriendlyName(device_key, tail + 2, remaining)) {
        /* build a string in the form: "COM1: (Friendly Name)" */
        tail[0] = _T(' ');
        tail[1] = _T('(');
        _tcscat(tail, _T(")"));
      }

      return true;
    }
  }

  /* virtual Bluetooth serial ports will not be found by the above;
     the following is necessary to enumerate those */

  while (bluetooth_ports.EnumKey(j++, key_name, 64)) {
    RegistryKey port(bluetooth_ports, key_name, true);
    if (port.error())
      continue;

    if (!port.GetValue(_T("Port"), name.buffer(), name.MAX_SIZE - 1))
      continue;

    /* the trailing colon is missing in this part of the registry */
    name.Append(_T(':'));

    display_name = name;

    /* see if we can find a human-readable name */
    const TCHAR *kn = key_name;
    RegistryKey device(bluetooth_device, kn, true);
    while (device.error() && *kn == _T('0')) {
      /* turns out Windows CE strips four leading zeroes for the
         Bluetooth\Device\* key (12 digits instead of 16); this is an
         attempt to kludge around this weirdness */
      ++kn;
      device = RegistryKey(bluetooth_device, kn, true);
    }

    if (!device.error()) {
      const size_t length = display_name.length();
      TCHAR *const tail = display_name.buffer() + length;
      const size_t remaining = display_name.MAX_SIZE - length - 3;

      if (device.GetValue(_T("name"), tail + 2, remaining)) {
        /* build a string in the form: "COM1: (Friendly Name)" */
        tail[0] = _T(' ');
        tail[1] = _T('(');
        _tcscat(tail, _T(")"));
      }
    }

    return true;
  }

  return false;
}