list_win32.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (c) 2013-2023 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <objbase.h>
  20. #include <initguid.h>
  21. #include <setupapi.h>
  22. extern "C" {
  23. // http://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx
  24. const GUID guids[] = {
  25. // COM & LPT ports
  26. {0x4D36E978, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18}},
  27. // Modem
  28. {0x4D36E96D, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18}},
  29. // Bluetooth
  30. {0xE0CBF06C, 0xCD8B, 0x4647, {0xBB, 0x8A, 0x26, 0x3B, 0x43, 0xF0, 0xF9, 0x74}},
  31. // com0com virtual ports
  32. {0xDF799E12, 0x3C56, 0x421B, {0xB2, 0x98, 0xB6, 0xD3, 0x64, 0x2B, 0xC8, 0x78}}
  33. };
  34. #include "blitz.h"
  35. BBObject * io_serial_TSerialPortInfo__create(BBString * portName, BBString * physicalName, BBString * productName,
  36. BBString * enumeratorName, int vendorId, int productId);
  37. void io_serial_TSerialPortInfo__addInfo(BBObject * list, BBObject * info);
  38. void io_serial_TSerialPortInfo__getIds(BBString * hids, int * vendorId, int * productId);
  39. void bmx_serial_listports(BBObject * list);
  40. }
  41. void bmx_serial_listports(BBObject * list) {
  42. int size = sizeof(guids)/sizeof(guids[0]);
  43. for (int i = 0; i < size; i++) {
  44. HDEVINFO deviceInfo;
  45. if ((deviceInfo = SetupDiGetClassDevs(&guids[i], NULL, NULL, DIGCF_PRESENT)) != INVALID_HANDLE_VALUE) {
  46. SP_DEVINFO_DATA deviceInfoData;
  47. deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  48. int device = 0;
  49. while (SetupDiEnumDeviceInfo(deviceInfo, device, &deviceInfoData)) {
  50. device++;
  51. BBString * bbPortName = &bbEmptyString;
  52. BBString * bbPhysicalName = &bbEmptyString;
  53. BBString * bbProductName = &bbEmptyString;
  54. BBString * bbEnumeratorName = &bbEmptyString;
  55. int bbVendorId = 0;
  56. int bbProductId = 0;
  57. DWORD bufferSize = 0;
  58. BYTE *buffer = 0;
  59. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &bufferSize);
  60. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  61. buffer = new BYTE[bufferSize];
  62. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, NULL, buffer, bufferSize, NULL);
  63. bbProductName = bbStringFromUTF8String((unsigned char*)buffer);
  64. delete [] buffer;
  65. }
  66. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, NULL, NULL, 0, &bufferSize);
  67. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  68. buffer = new BYTE[bufferSize];
  69. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, NULL, buffer, bufferSize, NULL);
  70. bbPhysicalName = bbStringFromUTF8String((unsigned char*)buffer);
  71. delete [] buffer;
  72. }
  73. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_ENUMERATOR_NAME, NULL, NULL, 0, &bufferSize);
  74. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  75. buffer = new BYTE[bufferSize];
  76. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_ENUMERATOR_NAME, NULL, buffer, bufferSize, NULL);
  77. bbEnumeratorName = bbStringFromUTF8String((unsigned char*)buffer);
  78. delete [] buffer;
  79. }
  80. HKEY devKey = SetupDiOpenDevRegKey(deviceInfo, &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
  81. bufferSize = 0;
  82. DWORD type;
  83. if (RegQueryValueEx(devKey, TEXT("PortName"), NULL, NULL, NULL, &bufferSize) == ERROR_SUCCESS) {
  84. buffer = new BYTE[size];
  85. if (RegQueryValueEx(devKey, TEXT("PortName"), NULL, &type, buffer, &bufferSize) == ERROR_SUCCESS) {
  86. bbPortName = bbStringFromUTF8String((unsigned char*)buffer);
  87. }
  88. RegCloseKey(devKey);
  89. delete [] buffer;
  90. }
  91. BBString * hids = &bbEmptyString;
  92. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_HARDWAREID, NULL, NULL, 0, &bufferSize);
  93. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  94. buffer = new BYTE[bufferSize];
  95. SetupDiGetDeviceRegistryProperty(deviceInfo, &deviceInfoData, SPDRP_HARDWAREID, NULL, buffer, bufferSize, NULL);
  96. hids = bbStringFromUTF8String((unsigned char*)buffer);
  97. delete [] buffer;
  98. }
  99. if (hids != &bbEmptyString) {
  100. io_serial_TSerialPortInfo__getIds(hids, &bbVendorId, &bbProductId);
  101. }
  102. BBObject * info = io_serial_TSerialPortInfo__create(bbPortName, bbPhysicalName, bbProductName, bbEnumeratorName, bbVendorId, bbProductId);
  103. if (info && (info != &bbNullObject)) {
  104. io_serial_TSerialPortInfo__addInfo(list, info);
  105. }
  106. }
  107. SetupDiDestroyDeviceInfoList(deviceInfo);
  108. }
  109. }
  110. }