谢欣伦 - 化繁为简系列原创教程 - 通信专题 - 设备查找类CxDeviceFind & CxDeviceMapFind

  这是一个精练的设备查找类,类名、函数名和变量名均采用匈牙利命名法。小写的x代表我的姓氏首字母(谢欣伦),个人习惯而已,如有雷同,纯属巧合。

CxDeviceFind的使用如下:

void CUsbScannerDlg::SearchDevice(LPGUID lpguidDev)

{

    m_list1.ResetContent();



    HANDLE hDevice;

    TCHAR szPath[MAX_PATH] = {0};

    GUID guidCls;

    CxDeviceFind finder;     BOOL bRet = finder.FindDevice(lpguidDev); while (bRet)

    {

        finder.GetDevicePath(szPath);         m_list1.AddString(szPath);



        hDevice = CreateFile(szPath, GENERIC_READ | GENERIC_WRITE, 0,

                                NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

        if (hDevice != INVALID_HANDLE_VALUE)

        {

            CloseHandle(hDevice);

            m_list1.AddString(_T("Can be open"));

        }

        else

            m_list1.AddString(_T("Can not be open"));

        

        finder.GetDeviceFriendlyName(szPath);         m_list1.AddString(szPath);

        

        finder.GetDeviceLocationInfo(szPath);         m_list1.AddString(szPath);

        

        finder.GetDeviceDescription(szPath);         m_list1.AddString(szPath);

        

        finder.GetDeviceHardwareID(szPath);         m_list1.AddString(szPath);

        

        finder.GetDeviceInstanceID(szPath);         m_list1.AddString(szPath);

        

        finder.GetClassGuid(&guidCls);         sprintf(szPath, "guid_cls[%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x]",

            guidCls.Data1, guidCls.Data2, guidCls.Data3,

            guidCls.Data4[0], guidCls.Data4[1], guidCls.Data4[2], guidCls.Data4[3],

            guidCls.Data4[4], guidCls.Data4[5], guidCls.Data4[6], guidCls.Data4[7]);

        m_list1.AddString(szPath);

        

        m_list1.AddString("");

        m_list1.AddString("-->Next<--");

        bRet = finder.FindNextDevice();     }

    finder.Close(); }

然后在需要的地方调用此函数,例如:

void CUsbScannerDlg::OnBtnUsb() 

{

    // TODO: Add your control notification handler code here

    GUID guid = GUID_DEVINTERFACE_USB_DEVICE;

    SearchDevice(&guid); }

其中 GUID guid 可以是以下多种设备类型的GUID
GUID_DEVINTERFACE_USB_DEVICE

GUID_DEVINTERFACE_COMPORT

GUID_DEVINTERFACE_PARALLEL

GUID_BTHPORT_DEVICE_INTERFACE

等等,更多设备类型的GUID可参见MSDN。

CxDeviceMapFind的使用如下:

int xApi::GetInstalledSerialPort(LPBYTE lpbtPort, int nMaxCount)

{

    int nPrefixLen = lstrlen(_T("COM")), nSerialPort, i = 0, j = 0;

    TCHAR szPath[MAX_PATH] = {0};
CxDeviceMapFind dev_map_find; GUID guid
= GUID_DEVINTERFACE_COMPORT; BOOL bRet = dev_map_find.FindDeviceMap(&guid); while (bRet) { dev_map_find.GetDevicePath(szPath); if (lstrlen(szPath) > nPrefixLen) { nSerialPort = _ttoi(&szPath[nPrefixLen]); if (i < nMaxCount) lpbtPort[i++] = nSerialPort; j++; } bRet = dev_map_find.FindNextDeviceMap(); } dev_map_find.Close(); return j; }

然后在需要的地方调用此函数,例如:

void CCaloricControllerDlg::OnDropdownCmbSerial() 

{

    // TODO: Add your control notification handler code here

    m_cmbSerial.ResetContent();

    CString str;

    BYTE btPort[MAXBYTE];

    int nCount = xApi::GetInstalledSerialPort(btPort, MAXBYTE);

    m_cmbSerial.ResetContent();

    for (int i=0; i<nCount; i++)

    {

        str.Format(_T("COM%d"), (int)btPort[i]);

        m_cmbSerial.AddString(str);

    }    

}

目前CxDeviceMapFind只支持以下GUID

GUID_DEVINTERFACE_COMPORT

GUID_DEVINTERFACE_PARALLEL

  精练的代码不需要过多解释,你们懂的。To be continued...

下载

libComm - v1.2 For WinXP/Win7

你可能感兴趣的:(device)