Список устройтсв в Win9X
От: nugb00t  
Дата: 26.09.03 10:18
Оценка:
Привет.

Нужно определить список устройств под Win9X и вывести его в XML. Получился такой код:

CString GetDeviceListCM()
{
    CString        sResult;

    try
    {
        sResult = _T("\t<devices>\n");

        TCHAR        buf[16384];  // XXXXX How big does this have to be? Dynamically size it?
        DEVINST        devInst;
        DEVINST        devInstNext;
        CONFIGRET    cr;
        BOOL        bDone = 0;
        ULONG        len;

        // Get Root DevNode
        cr = CM_Locate_DevNode(&devInst, NULL, 0);
        if (cr != CR_SUCCESS)
            throw cr;

        // Do a depth first search for the DevNode with a matching DriverName value
        while (!bDone)
        {
            // Get the DriverName value
            len = sizeof(buf);
            cr = CM_Get_DevNode_Registry_Property(devInst, CM_DRP_DRIVER, NULL, buf, &len, 0);

            // If the DriverName value matches, return the DeviceDescription
            if (cr == CR_SUCCESS)
            {
                len = sizeof(buf);
                cr = CM_Get_DevNode_Registry_Property(devInst, CM_DRP_DEVICEDESC, NULL, buf, &len, 0);

                if (cr == CR_SUCCESS)
                    sResult.AppendFormat(_T("\t\t<item>%s</item>\n"), this->EncodeNonAlpha(CString(buf)));
                else
                    break;
            }

            // This DevNode didn't match, go down a level to the first child.
            cr = CM_Get_Child(&devInstNext, devInst, 0);
            if (cr == CR_SUCCESS)
            {
                devInst = devInstNext;
                continue;
            }

            // Can't go down any further, go across to the next sibling.  If
            // there are no more siblings, go back up until there is a sibling.
            // If we can't go up any further, we're back at the root and we're
            // done.
            for (;;)
            {
                cr = CM_Get_Sibling(&devInstNext, devInst, 0);
                if (cr == CR_SUCCESS)
                {
                    devInst = devInstNext;
                    break;
                }

                cr = CM_Get_Parent(&devInstNext, devInst, 0);
                if (cr == CR_SUCCESS)
                    devInst = devInstNext;
                else
                {
                    bDone = TRUE;
                    break;
                }
            }            
        }
        sResult += _T("\t</devices>\n");
    }
    catch (CONFIGRET crXc)
    {
        sResult.Format(_T("\t<devices>Error: %d</devices>\n"), crXc);
    }
    catch (...)
    {
        sResult = _T("\t<devices>Error: GetDeviceListCM() failed.</devices>\n");
    }
    
    return sResult;
}


Код работает под XP, а под w95 и w98 прога требует библиотеку 'CFGMGR32.DLL'. Нашел её в апдейте w95ws2setup.exe (winsock 2 похоже). После этого функция CM_Locate_DevNode() возвращает ошибку 0x2C (CR_NO_DEPENDENT). Cборка без UNICODE.
... << RSDN@Home 1.1 beta 2 >>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.