不复杂,直接上C代码:
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <atlstr.h>
using namespace std;
int main()
{
CString strCom, strComOpen;
int nCom = 0;
int count = 0;
HANDLE hCom;
do
{
nCom++;
strCom.Format("COM%d", nCom);
strComOpen.Format("\\\\.\\COM%d", nCom);
MessageBox(NULL, strComOpen, "", MB_OK);
hCom = CreateFile(strComOpen, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (INVALID_HANDLE_VALUE == hCom)
{
DWORD error = ::GetLastError();
}
else
{
MessageBox(NULL, strCom, "", MB_OK);
count++;
}
CloseHandle(hCom);
} while (nCom<10);
return 0;
}