Best approach to get active ports from hardware oriented language like c, c++. in c we can make code snippet like
Try QueryDosDevice, e.g.
TCHAR szDevices[65535];
unsigned long dwChars = QueryDosDevice(NULL, szDevices, 65535);
TCHAR *ptr = szDevices;
while (dwChars)
{
int port;
if (sscanf(ptr, "COM%d", &port) == 1)
{
}
TCHAR *temp_ptr = strchr(ptr, 0);
dwChars -= (DWORD)((temp_ptr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}