forked from daynix/UsbDk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UsbDkHelper: Introduce DeviceMgr class with reset logic
Signed-off-by: Pavel Gurvich <[email protected]> Signed-off-by: Dmitry Fleytman <[email protected]>
- Loading branch information
Pavel Gurvich
authored and
Dmitry Fleytman
committed
May 11, 2014
1 parent
7fa28c0
commit 998fd9a
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "stdafx.h" | ||
#include "DeviceMgr.h" | ||
|
||
#include <cfgmgr32.h> | ||
|
||
InstallResult DeviceMgr::ResetDeviceByClass(const GUID &ClassGuid) | ||
{ | ||
auto hDevInfo = SetupDiGetClassDevsEx(&ClassGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, NULL, NULL, NULL); | ||
if (hDevInfo == INVALID_HANDLE_VALUE) | ||
{ | ||
throw UsbDkDeviceMgrException(TEXT("DeviceMgr throw the exception: SetupDiGetClassDevsEx() failed!!")); | ||
} | ||
|
||
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail; | ||
devInfoListDetail.cbSize = sizeof(devInfoListDetail); | ||
if (!SetupDiGetDeviceInfoListDetail(hDevInfo, &devInfoListDetail)) | ||
{ | ||
SetupDiDestroyDeviceInfoList(hDevInfo); | ||
throw UsbDkDeviceMgrException(TEXT("DeviceMgr throw the exception: SetupDiGetDeviceInfoListDetail() failed!!")); | ||
} | ||
|
||
InstallResult installRes = InstallSuccess; | ||
SP_DEVINFO_DATA devInfo; | ||
devInfo.cbSize = sizeof(devInfo); | ||
for (DWORD devIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, devIndex, &devInfo); devIndex++) | ||
{ | ||
installRes = ResetDevice(hDevInfo, &devInfo, &devInfoListDetail); | ||
if (installRes != InstallSuccess) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
SetupDiDestroyDeviceInfoList(hDevInfo); | ||
|
||
return installRes; | ||
} | ||
//-------------------------------------------------------------------------------- | ||
|
||
InstallResult DeviceMgr::ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo, PSP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail) | ||
{ | ||
InstallResult installRes = InstallSuccess; | ||
|
||
SP_PROPCHANGE_PARAMS pcParams; | ||
pcParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER); | ||
pcParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; | ||
pcParams.StateChange = DICS_PROPCHANGE; | ||
pcParams.Scope = DICS_FLAG_CONFIGSPECIFIC; | ||
pcParams.HwProfile = 0; | ||
|
||
if (!SetupDiSetClassInstallParams(devs, devInfo, &pcParams.ClassInstallHeader, sizeof(pcParams)) || | ||
!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, devs, devInfo)) | ||
{ | ||
installRes = InstallFailureNeedReboot; | ||
} | ||
else | ||
{ | ||
SP_DEVINSTALL_PARAMS devInstallParams; | ||
devInstallParams.cbSize = sizeof(devInstallParams); | ||
if (SetupDiGetDeviceInstallParams(devs, devInfo, &devInstallParams) && (devInstallParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))) | ||
{ | ||
installRes = InstallFailureNeedReboot; | ||
} | ||
} | ||
|
||
return installRes; | ||
} | ||
//-------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <Setupapi.h> | ||
#include "UsbDkHelper.h" | ||
|
||
//----------------------------------------------------------------------------------- | ||
|
||
class UsbDkDeviceMgrException : public UsbDkW32ErrorException | ||
{ | ||
public: | ||
UsbDkDeviceMgrException() : UsbDkW32ErrorException(TEXT("DeviceMgr throw the exception")){} | ||
UsbDkDeviceMgrException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(lpzMessage){} | ||
}; | ||
//----------------------------------------------------------------------------------- | ||
|
||
class DeviceMgr | ||
{ | ||
public: | ||
static InstallResult ResetDeviceByClass(const GUID &ClassGuid); | ||
private: | ||
static InstallResult ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo, PSP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail); | ||
}; | ||
//----------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters