现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC 枚举电脑串口二法

2013-11-22 23:46 工业·编程 ⁄ 共 3435字 ⁄ 字号 暂无评论

1.从注册表读取信息:
代码如下:
从注册表读取串口

void CCommDlg::EnumCommPort(CListBox *pListBox)
{
    HKEY hKey;
    if(ERROR_SUCCESS ==::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm",NULL, KEY_READ, &hKey ))//打开串口注册表对应的键值
    {
        int i=0;
        TCHAR RegKeyName[128],SerialPortName[128];
        DWORD dwLong,dwSize;
        while(TRUE)
        {
            dwLong = dwSize=sizeof(RegKeyName);
            if(ERROR_NO_MORE_ITEMS==::RegEnumValue(hKey,i,RegKeyName,&dwLong,NULL,NULL,(PUCHAR)SerialPortName, &dwSize))//枚举串口
            {
                break;
            }
            if(!memcmp(RegKeyName,"\\Device\\",8))//过滤虚拟串口
            {
                pListBox->AddString(SerialPortName); //SerialPortName就是串口名字
            }
            i++;
        }
        if(pListBox->GetCount()==0)
        {
            MessageBox("在HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm里找不到串口!!!");
        }
        RegCloseKey(hKey);
    }
    else
    {
        RegCloseKey(hKey);
        MessageBox("您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项");
    }
}

2.利用windows SDK 库函数读取硬件信息
代码如下:
利用windows SDK 库函数读取硬件信息

unsigned char CCommDlg::DeviceEnumCommPort(CListBox *pListBox)
{
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD DeviceIndex;
    //Create a HDEVINFO with all present devices.
    hDevInfo=SetupDiGetClassDevs((LPGUID) &GUID_DEVCLASS_PORTS,0,0,DIGCF_PRESENT);

    /*
    GUID_DEVCLASS_FDC软盘控制器
    GUID_DEVCLASS_DISPLAY显示卡
    GUID_DEVCLASS_CDROM光驱
    GUID_DEVCLASS_KEYBOARD键盘
    GUID_DEVCLASS_COMPUTER计算机
    GUID_DEVCLASS_SYSTEM系统
    GUID_DEVCLASS_DISKDRIVE磁盘驱动器
    GUID_DEVCLASS_MEDIA声音、视频和游戏控制器
    GUID_DEVCLASS_MODEMMODEM
    GUID_DEVCLASS_MOUSE鼠标和其他指针设备
    GUID_DEVCLASS_NET网络设备器
    GUID_DEVCLASS_USB通用串行总线控制器
    GUID_DEVCLASS_FLOPPYDISK软盘驱动器
    GUID_DEVCLASS_UNKNOWN未知设备
    GUID_DEVCLASS_SCSIADAPTERSCSI 和 RAID 控制器
    GUID_DEVCLASS_HDCIDE ATA/ATAPI 控制器
    GUID_DEVCLASS_PORTS端口(COM 和360非主流 LPT)
    GUID_DEVCLASS_MONITOR监视器
    */

    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
        DWORD ErrorCode = GetLastError();
        // Insert error handling here.
        return 1;
    }
    // Enumerate through all devices in Set.

    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
   
    for(DeviceIndex=0;SetupDiEnumDeviceInfo(hDevInfo,DeviceIndex,&DeviceInfoData);DeviceIndex++)
    {
        DWORD DataPropertyType;
        LPTSTR buffer = NULL;
        DWORD buffersize = 0;
        //
        // Call function with null to begin with,
        // then use the returned buffer size
        // to Alloc the buffer. Keep calling until
        // success or an unknown failure.
        //
        while (!SetupDiGetDeviceRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_FRIENDLYNAME,&DataPropertyType,(PBYTE)buffer,buffersize,&buffersize))
        {
            DWORD ErrorCode=GetLastError();
            if (ErrorCode == ERROR_INSUFFICIENT_BUFFER)
            {
                // Change the buffer size.
                if (buffer) LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
            }
            else
            {
                // Insert error handling here.
                break;
            }
        }
        CString FriendlyPortName=buffer; /*Extract Serial Port Name*/
        int pos=FriendlyPortName.Find('(');
        CString SerialPortName=FriendlyPortName.Right(FriendlyPortName.GetLength()-pos-1);
        SerialPortName.TrimRight(')');
        pListBox->AddString(SerialPortName);
        if(buffer)
        {
            LocalFree(buffer);
        }
    }
    if (GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS)
    {
        return 1;
    }
    // Cleanup
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return 0;
}

给我留言

留言无头像?