Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1680653
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2012-02-13 15:45:26


  1. #include    
    #include    
    #include    
    #include     


  2. BOOL GetHardwareID(HDEVINFO hDev, SP_DEVINFO_DATA& DeviceInfoData, LPCTSTR pstrHardwareID, int nSize);



  3. #define MAX_CLASS_NAME_LEN 128

  4. extern "C" DWORD WINAPI InstallDriver(LPCTSTR INFFile)
  5. {
  6.   GUID guid;
  7.   TCHAR szClassName[MAX_CLASS_NAME_LEN];
  8.   TCHAR szHardwareID[256];
  9.   DWORD dwError = 0;

  10.   memset(szHardwareID, 0, sizeof(szHardwareID));

  11.   if (!::SetupDiGetINFClass(INFFile, &guid, szClassName, MAX_CLASS_NAME_LEN, NULL))
  12.   {
  13.     dwError = GetLastError();

  14.     return dwError;
  15.   }

  16.   HDEVINFO hDev = INVALID_HANDLE_VALUE;
  17.   SP_DEVINFO_DATA DeviceInfoData;
  18.   DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

  19.   hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE);

  20.   if (hDev != INVALID_HANDLE_VALUE)
  21.   {
  22.     hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);

  23.     if (hDev != INVALID_HANDLE_VALUE)
  24.     {
  25.       // 设备已经安装,先删除
  26.       if (GetHardwareID(hDev, DeviceInfoData, szHardwareID, 255))
  27.       {
  28.         ::SetupDiCallClassInstaller(DIF_REMOVE, hDev, &DeviceInfoData);
  29.       }
  30.     }

  31.     ::SetupDiDestroyDeviceInfoList(hDev);
  32.   }

  33.   if (szHardwareID[0] == _T('\x0') )
  34.   {
  35.     // 没有得到HardwareID,使用ClassName
  36.     szHardwareID[0] = _T('*');
  37. #ifdef _UNICODE
  38.     wcscpy(szHardwareID+1, szClassName);
  39.     _wcslwr(szHardwareID);
  40. #else
  41.     strcpy(szHardwareID+1, szClassName);
  42.     _strlwr(szHardwareID);
  43. #endif
  44.   }

  45.   BOOL bSuccess = FALSE;

  46.   hDev = SetupDiCreateDeviceInfoList(&guid, NULL);
  47.   if (hDev != INVALID_HANDLE_VALUE)
  48.   {
  49.     if (::SetupDiCreateDeviceInfo(hDev, szClassName, &guid, NULL, NULL, DICD_GENERATE_ID, &DeviceInfoData))
  50.     {
  51.       if (::SetupDiSetDeviceRegistryProperty(hDev,
  52.         &DeviceInfoData,
  53.         SPDRP_HARDWAREID,
  54.         (LPBYTE)szHardwareID,
  55.         (
  56. #ifdef _UNICODE
  57.         wcslen(szHardwareID)
  58. #else
  59.         strlen(szHardwareID)
  60. #endif
  61.         + 2 ) * sizeof(TCHAR) ) )
  62.       {
  63.         bSuccess = ::SetupDiCallClassInstaller(DIF_REGISTERDEVICE, hDev, &DeviceInfoData);
  64.       }
  65.     }
  66.   }
  67.   dwError = GetLastError();

  68.   ::SetupDiDestroyDeviceInfoList(hDev);

  69.   if (bSuccess)
  70.   {
  71.     BOOL bReBoot;

  72.     bSuccess = UpdateDriverForPlugAndPlayDevices(NULL,
  73.       szHardwareID,
  74.       INFFile,
  75.       1,
  76.       &bReBoot);

  77.     dwError = bSuccess ? 0 : GetLastError();
  78.   }

  79.   return dwError;
  80. }

  81. extern "C" DWORD WINAPI UninstallDriver(LPCTSTR INFFile)
  82. {
  83.   GUID guid;
  84.   TCHAR szClassName[MAX_CLASS_NAME_LEN];
  85.   TCHAR szHardwareID[256];
  86.   DWORD dwError = 0;

  87.   memset(szHardwareID, 0, sizeof(szHardwareID));

  88.   if (!::SetupDiGetINFClass(INFFile, &guid, szClassName, MAX_CLASS_NAME_LEN, NULL))
  89.   {
  90.     dwError = GetLastError();
  91.     return dwError;
  92.   }

  93.   HDEVINFO hDev = INVALID_HANDLE_VALUE;
  94.   SP_DEVINFO_DATA DeviceInfoData;
  95.   DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

  96.   hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE);

  97.   if (hDev != INVALID_HANDLE_VALUE)
  98.   {
  99.     hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);

  100.     if (hDev != INVALID_HANDLE_VALUE)
  101.     {
  102.       // 设备已经安装,先删除
  103.       if (GetHardwareID(hDev, DeviceInfoData, szHardwareID, 255))
  104.       {
  105.         if (!::SetupDiCallClassInstaller(DIF_REMOVE, hDev, &DeviceInfoData) )
  106.           dwError = -1;
  107.       }
  108.     }

  109.     ::SetupDiDestroyDeviceInfoList(hDev);
  110.   }

  111.   return dwError;
  112. }

  113. BOOL GetHardwareID(HDEVINFO hDev, SP_DEVINFO_DATA& DeviceInfoData, LPCTSTR pstrHardwareID, int nSize)
  114. {
  115.   int nIndex = 0;

  116.   if (::SetupDiEnumDeviceInfo(hDev, nIndex, &DeviceInfoData))
  117.   {
  118.     DWORD dwSize = nSize;
  119.     DWORD dwType;

  120.     if (::SetupDiGetDeviceRegistryProperty(hDev, &DeviceInfoData, SPDRP_HARDWAREID,
  121.       &dwType, (LPBYTE)pstrHardwareID, nSize, &dwSize))
  122.     {
  123.       if (!::SetupDiGetDeviceRegistryProperty(hDev,
  124.         &DeviceInfoData,
  125.         SPDRP_HARDWAREID,
  126.         &dwType,
  127.         (LPBYTE)pstrHardwareID,
  128.         nSize,
  129.         &dwSize))
  130.       {
  131.         return FALSE;
  132.       }
  133.     }
  134.     else
  135.     {
  136.       return FALSE;
  137.     }
  138.   }
  139.   else
  140.   {
  141.     return FALSE;
  142.   }

  143.   return TRUE;
  144. }
  其中newdev.h是DDK里面的,如果没有安装也可以自己从newdev.dll获取UpdateDriverForPlugAndPlayDevices的地址
阅读(3141) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~