例如u盘和网卡的,请给出相关函数

解决方案 »

  1.   

    看一下下面的例子,这是我在别的地方看到的,希望对你有帮助:
    #include <stdio.h>
    #include <windows.h>
    #include <setupapi.h>
    #include <devguid.h>
    #include <regstr.h>#pragma comment(lib,"Setupapi.lib")
    int main( )
    {
        HDEVINFO hDevInfo;
        SP_DEVINFO_DATA DeviceInfoData;
    SP_DEVICE_INTERFACE_DATA  DeviceInterfaceData;    DWORD i;
        GUID aa;
    GUID *ab;
    ab=&aa;
    i=sizeof(GUID);
        DWORD DataT;
    char buf[255];
    memset(buf,0,256);
        LPTSTR buffer = buf;
        DWORD buffersize = 0;
        if(!SetupDiClassGuidsFromName("USB",ab,i,&i))//通过设备类名得到设备类型的GUID 可以改变“USB”为“NET”或者其他设备名称
    {
      printf("false");
      return 1;
    }
        hDevInfo = SetupDiGetClassDevs(ab, 0, 0, 0);//DIGCF_PRESENT | DIGCF_ALLCLASSES );//通过设备类型GUID得到这类设备信息的句柄
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            // Insert error handling here.
            return 1;
        }
           
        // Enumerate through all devices in Set.
    printf("GUID={%X-%X-%X-%X%X-%X%X%X%X%X%X}\n",ab->Data1,ab->Data2,ab->Data3,ab->Data4[0],ab->Data4[1],ab->Data4[2],ab->Data4[3],ab->Data4[4],ab->Data4[5],ab->Data4[6],ab->Data4[7]);
        DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        for (i = 0;SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)//枚举这类设备中所有的设备
        {
            for(int s=0;s<2;s++)//因为有些时候第一次调用SetupDiGetDeviceRegistryProperty可能会失败,所以调用两次,
        {
           SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
    &DataT, (PBYTE)buffer, buffersize, &buffersize);//得到设备属性
    }

            printf("设备: [ %s size=%d ]\n",buffer,buffersize);
            memset(buf,0,256);   for (int m = 0;
     SetupDiEnumDeviceInterfaces(hDevInfo,
     &DeviceInfoData,
     ab,m,&DeviceInterfaceData); m++)//枚举这类设备中所有的设备
    {
               printf("GUID={%X-%X-%X-%X%X-%X%X%X%X%X%X}\n",
       DeviceInterfaceData.InterfaceClassGuid.Data1,
       DeviceInterfaceData.InterfaceClassGuid.Data2,
       DeviceInterfaceData.InterfaceClassGuid.Data3,
       DeviceInterfaceData.InterfaceClassGuid.Data4[0],
       DeviceInterfaceData.InterfaceClassGuid.Data4[1],
       DeviceInterfaceData.InterfaceClassGuid.Data4[2],
       DeviceInterfaceData.InterfaceClassGuid.Data4[3],
       DeviceInterfaceData.InterfaceClassGuid.Data4[4],
       DeviceInterfaceData.InterfaceClassGuid.Data4[5],
       DeviceInterfaceData.InterfaceClassGuid.Data4[6],
       ab->Data4[7]);
            printf("设备: [ %s size=%d ]\n",buffer,buffersize);
            memset(buf,0,256);
              }   
    //  printf("Do you want close this driver (YES)y or (NO)n\n");
    //  char ch[128];
    //  scanf("%s",ch);
    // if(ch[0]=='y')
    // {
    /* if(SetupDiRemoveDevice(hDevInfo, &DeviceInfoData))
    {
    printf("SetupDiRemoveDevice succeed");
    }else
    {
    printf("SetupDiRemoveDevice false");
    }*/
    // }    }     if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
        {
            // Insert error handling here.
            return 1;
        }
           
        //  Cleanup
        SetupDiDestroyDeviceInfoList(hDevInfo);
           
        return 0;
    }