Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1111927
  • 博文数量: 143
  • 博客积分: 969
  • 博客等级: 准尉
  • 技术积分: 1765
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-30 12:09
文章分类

全部博文(143)

文章存档

2023年(4)

2021年(2)

2020年(4)

2019年(4)

2018年(33)

2017年(6)

2016年(13)

2014年(7)

2013年(23)

2012年(33)

2011年(14)

我的朋友

分类: C/C++

2011-09-10 16:47:48

  1. bool IsDeviceDisabled(DWORD dwDevID, HDEVINFO hDevInfo, DWORD &dwStatus)
  2. {
  3.     SP_DEVINFO_DATA DevInfoData = {sizeof(SP_DEVINFO_DATA)};
  4.     DWORD dwDevStatus,dwProblem;
  5.     if(!SetupDiEnumDeviceInfo(hDevInfo,dwDevID,&DevInfoData))
  6.     {
  7.         return FALSE;
  8.     }

  9.     //查询设备状态
  10.     if(CM_Get_DevNode_Status(&dwDevStatus,&dwProblem,DevInfoData.DevInst,0)!=CR_SUCCESS)
  11.     {
  12.         return FALSE;
  13.     }
  14.     
  15.     dwStatus = dwProblem;
  16. //    return ( (dwProblem == CM_PROB_FAILED_INSTALL));

  17.     return true;
  18. }

  19. int IsInstallDriver()
  20. {
  21.     HDEVINFO hDevInfo;
  22.     SP_DEVINFO_DATA DeviceInfoData;
  23.     DWORD i;
  24.     bool bRet = false;
  25.     bool bOk = false;

  26.       //step1. Create a HDEVINFO with all present devices.
  27.     hDevInfo = SetupDiGetClassDevs(NULL,
  28.         0, // Enumerator
  29.         0,
  30.         DIGCF_PRESENT | DIGCF_ALLCLASSES );

  31.     if (hDevInfo == INVALID_HANDLE_VALUE)
  32.     {
  33.         // Insert error handling here.
  34.         return bRet;
  35.     }

  36.      
  37.     DWORD dwStatuts = -1;
  38.     //step2. Enumerate through all devices in Set.
  39.     DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  40.     for (i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
  41.     {
  42.         DWORD DataT;
  43.         LPTSTR buffer = NULL;
  44.         DWORD buffersize = 0;
  45.    
  46.         // Call function with null to begin with,
  47.         // then use the returned buffer size
  48.         // to Alloc the buffer. Keep calling until
  49.         // success or an unknown failure.
  50.         //
  51.         while (!SetupDiGetDeviceRegistryProperty(
  52.             hDevInfo,
  53.             &DeviceInfoData,
  54.             SPDRP_HARDWAREID,
  55.             &DataT,
  56.             (PBYTE)buffer,
  57.             buffersize,
  58.             &buffersize))
  59.         {
  60.             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  61.             {
  62.                 // Change the buffer size.
  63.                 if (buffer) LocalFree(buffer);
  64.                 buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
  65.             }
  66.             else
  67.             {
  68.                 // Insert error handling here.
  69.                 break;
  70.             }
  71.         }

  72.         printf("%S\n", buffer);
  73.         //step3. find devices status
  74.         if (buffer && (!wcscmp(buffer,L"USB\\Vid_0955&Pid_7103") || !wcscmp(buffer,L"USB\\Vid_0955&Pid_7102")) )
  75.         {
  76.             
  77.             if (IsDeviceDisabled(i, hDevInfo, dwStatuts) && dwStatuts == 0)
  78.             {
  79.     
  80.             }
  81.             //printf( "SPDRP_DEVICEDESC:[%S] %d\n ",buffer, dwStatuts);

  82.             if (buffer)
  83.                 LocalFree(buffer);
  84.             break;
  85.         }

  86.         if (buffer)
  87.             LocalFree(buffer);
  88.     }

  89.     // step4. Cleanup
  90.     SetupDiDestroyDeviceInfoList(hDevInfo);
  91.     
  92.     return dwStatuts;
  93. }
阅读(8332) | 评论(0) | 转发(0) |
0

上一篇:委托模式

下一篇:lsof

给主人留下些什么吧!~~