我要作的程序的功能是:检测计算机有几个usb口,检测到后提示插入一个usb设备,只要系统能认到就可以了,应该不是很难的程序,但是我以前没接触过这东西,有没有作过这方面的朋友帮帮忙啊,谢谢了!!!

解决方案 »

  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;
        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
      {
      printf("false");
      return 1;
      }
       printf("端口设备GUID=%d-%d-%d-%d\n",ab->Data1,ab->Data2,ab->Data3,ab->Data4);
      
        hDevInfo = SetupDiGetClassDevs(ab, 0, 0, 0);//通过设备类型GUID得到这类设备信息的句柄
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            // Insert error handling here.
            return 1;
        }
           
        // Enumerate through all devices in Set.
         
        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);
                 } 
        if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
        {
            // Insert error handling here.
            return 1;
        }
           
        //  Cleanup
        SetupDiDestroyDeviceInfoList(hDevInfo);
           
        return 0;
    }我机器上的测试结果:端口设备GUID=922525280-50277-4559-1245012
    设备: [ Intel(R) 82801BA/BAM USB Universal Host Controller - 2442 size=58 ]
    设备: [ Intel(R) 82801BA/BAM USB Universal Host Controller - 2444 size=58 ]
    设备: [ USB Root Hub size=13 ]
    设备: [ USB Root Hub size=13 ]
    Press any key to continue
    改一下就能用了
      

  2.   

    楼上的兄弟,强啊!那我想从USB口读数据,该怎么办?