可用如下函数来进行操作
int EnumAllComPort(bool* bEnablePort)
{
int nCommSum = 0;//串口个数
HANDLE hCom;
CString str;
for(int i=1;i<=256;i++)
{
str.Format(_T("COM%d"),i);
hCom = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
bEnablePort[i-1] = false;
continue;
}
else
{
bEnablePort[i-1] = true;
}
CloseHandle(hCom);
nCommSum++;
}
return nCommSum;
}