当前位置:首页文章首页 IT学院 IT技术

全自动安装卸载驱动程序的源程序

作者:  来源:  发布时间:2011-12-15 0:53:30  点击:
   {

   return DisplayError(TEXT(CreateDeviceInfoList));

   }

   //

   // Now create the element.

   // Use the Class GUID and Name from the INF file.

   //

   DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

   if (!SetupDiCreateDeviceInfo(DeviceInfoSet,

   ClassName,

   &ClassGUID,

   NULL,

   0,

   DICD_GENERATE_ID,

   &DeviceInfoData))

   {

   DisplayError(TEXT(CreateDeviceInfo));

   goto cleanup_DeviceInfo;

   }

   //

   // Add the HardwareID to the Device's HardwareID property.

   //

   if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,

   &DeviceInfoData,

   SPDRP_HARDWAREID,

   (LPBYTE)HardwareId,

   (lstrlen(HardwareId)+1+1)*sizeof(TCHAR)))

   {

   DisplayError(TEXT(SetDeviceRegistryProperty));

   goto cleanup_DeviceInfo;

   }

   //

   // Transform the registry element into an actual devnode

   // in the PnP HW tree.

   //

   if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE,

   DeviceInfoSet,

   &DeviceInfoData))

   {

   DisplayError(TEXT(CallClassInstaller(REGISTERDEVICE)));

   goto cleanup_DeviceInfo;

   }

   //

   // The element is now registered. We must explicitly remove the

   // device using DIF_REMOVE, if we encounter any failure from now on.

   //

   //

   // Install the Driver.

   //

   if (!UpdateDriverForPlugAndPlayDevices(0,

   HardwareId,

   INFFile,

   INSTALLFLAG_FORCE,

   RebootRequired))

   {

   DWORD err = GetLastError();

   DisplayError(TEXT(UpdateDriverForPlugAndPlayDevices));

   if (!SetupDiCallClassInstaller(

   DIF_REMOVE,

   DeviceInfoSet,

   &DeviceInfoData))

   {

   DisplayError(TEXT(CallClassInstaller(REMOVE)));

   }

   SetLastError(err);

   }

   //

   // Cleanup.

   //

  cleanup_DeviceInfo:

   err = GetLastError();

   SetupDiDestroyDeviceInfoList(DeviceInfoSet);

   SetLastError(err);

   return err == NO_ERROR;

  }

  int InstallDriver(_TCHAR *InfName, _TCHAR *HardwareID)

  {

   WIN32_FIND_DATA FindFileData;

   BOOL RebootRequired = 0; // Must be cleared.

   _TCHAR *FName, *HWID;

   FName = InfName;

   HWID = HardwareID;

   if (FindFirstFile(FName,&FindFileData)==INVALID_HANDLE_VALUE)

   {

   //_tprintf(TEXT( File not found.\\n));

   //_tprintf(TEXT(usage: install \\n));

   return 2; // Install Failure

   }

   //

   // Look to see if this device allready exists.

   //

   if (FindExistingDevice(HWID))

   {

   //

   // No Need to Create a Device Node, just call our API.

   //

   if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle

   HWID, // Hardware ID

   FName, // FileName

   INSTALLFLAG_FORCE,

   &RebootRequired))

   {

   DisplayError(TEXT(UpdateDriverForPlugAndPlayDevices));

   return 2; // Install Failure

   }

   }

   else

   {

   if (GetLastError()!= ERROR_NO_MORE_ITEMS)

   {

   //

   // An unknown failure from FindExistingDevice()

   //

   //_tprintf(TEXT((IERROR_NO_MORE_ITEMS)\\n));

   //_tprintf(TEXT((Install Failure! Code = 2)\\n));

   return 2; // Install Failure

   }

   //

   // Driver Does not exist, Create and call the API.

   // HardwareID must be a multi-sz string, which argv[2] is.

   //

   if (!InstallRootEnumeratedDriver(HWID, // HardwareID

   FName, // FileName

   &RebootRequired))

   {

   //_tprintf(TEXT((InstallRootEnumeratedDriver Failure! Code = 2)\\n

  ));

   return 2; // Install Failure

   }

   }

   //_tprintf(TEXT(Driver Installed successfully.\\n));

   if (RebootRequired)

   {

   //_tprintf(TEXT((Reboot Required)\\n));

   return 1; // Install Success, reboot required.

   }

   return 0; // Install Success, no reboot required.

  }

  int RemoveDriver(_TCHAR *HardwareID)

  {

   HDEVINFO DeviceInfoSet;

   SP_DEVINFO_DATA DeviceInfoData;

   DWORD i,err;

   DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes

   0,

   0,

   DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system

   if (DeviceInfoSet == INVALID_HANDLE_VALUE)

   {

   DisplayError(TEXT(GetClassDevs(All Present Devices)));

   return 1;

   }

   //

   // Enumerate through all Devices.

   //

   DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

   for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)

首页 上一页 [1] [2] [3]  下一页 尾页
上一篇:USB 驱动自动卸载方法 下一篇:

文章评论

软件按字母排列: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z