diff --git a/UsbDkController/UsbDkController.cpp b/UsbDkController/UsbDkController.cpp index c60aa2f..0b399d0 100644 --- a/UsbDkController/UsbDkController.cpp +++ b/UsbDkController/UsbDkController.cpp @@ -48,6 +48,7 @@ static void ShowUsage() tcout << TEXT(" UsbDkController -H TYPE VID PID BCD Class Hide - add dynamic hide rule") << endl; tcout << TEXT(" UsbDkController -P TYPE VID PID BCD Class Hide - add persistent hide rule") << endl; tcout << TEXT(" UsbDkController -D TYPE VID PID BCD Class Hide - delete persistent hide rule") << endl; + tcout << TEXT(" UsbDkController -Z - delete all persistent hide rules") << endl; tcout << endl; tcout << TEXT(" May be specific value or -1 to match all") << endl; tcout << TEXT(" Should be 0 or 1, if 0, the rule is terminal") << endl; @@ -319,6 +320,15 @@ static void Controller_HideDevice(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BC UsbDk_CloseHiderHandle(hiderHandle); } +static int Controller_DeleteAllPersistentHideRules() +{ + ULONG done = 0, notDone = 0; + auto res = UsbDk_DeleteAllPersistentRules(&done, ¬Done); + tcout << TEXT("Cleaning persistent rules: done ") << dec << done + << TEXT(", not done ") << notDone << endl; + return Controller_AnalyzeInstallResult(res, TEXT("Clean persistent hide rules")); +} + static bool Controller_ChdirToPackageFolder() { TCHAR PackagePath[MAX_PATH]; @@ -410,6 +420,10 @@ int __cdecl _tmain(int argc, TCHAR* argv[]) } return Controller_DeletePersistentHideRule(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]); } + else if (_tcscmp(L"-Z", argv[1]) == 0) + { + return Controller_DeleteAllPersistentHideRules(); + } else { ShowUsage(); diff --git a/UsbDkHelper/RuleManager.cpp b/UsbDkHelper/RuleManager.cpp index 24845e2..3b20489 100644 --- a/UsbDkHelper/RuleManager.cpp +++ b/UsbDkHelper/RuleManager.cpp @@ -128,3 +128,20 @@ void CRulesManager::DeleteRule(const USB_DK_HIDE_RULE &Rule) } } } + +ULONG CRulesManager::DeleteAllRules(ULONG& notDeleted) +{ + ULONG deleted = 0; + notDeleted = 0; + vector subkeys; + + for (const auto &SubKey : m_RegAccess) + subkeys.push_back(SubKey); + + while (subkeys.size()) + { + m_RegAccess.DeleteKey(subkeys.front().c_str()) ? deleted++ : notDeleted++; + subkeys.erase(subkeys.begin()); + } + return deleted; +} diff --git a/UsbDkHelper/RuleManager.h b/UsbDkHelper/RuleManager.h index 424b565..70b100a 100644 --- a/UsbDkHelper/RuleManager.h +++ b/UsbDkHelper/RuleManager.h @@ -18,6 +18,7 @@ class CRulesManager void AddRule(const USB_DK_HIDE_RULE &Rule); void DeleteRule(const USB_DK_HIDE_RULE &Rule); + ULONG DeleteAllRules(ULONG& notDeleted); private: template bool FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor); diff --git a/UsbDkHelper/UsbDkHelper.cpp b/UsbDkHelper/UsbDkHelper.cpp index d4a9dbd..b6fa183 100644 --- a/UsbDkHelper/UsbDkHelper.cpp +++ b/UsbDkHelper/UsbDkHelper.cpp @@ -401,3 +401,29 @@ DLL InstallResult UsbDk_DeletePersistentHideRule(PUSB_DK_HIDE_RULE_PUBLIC Public { return UsbDk_DeleteExtendedPersistentHideRule(PublicRule, USBDK_HIDER_RULE_DEFAULT); } + +DLL InstallResult UsbDk_DeleteAllPersistentRules(OUT PULONG pDeleted, OUT PULONG pNotDeleted) +{ + try + { + CRulesManager Manager; + + *pDeleted = *pNotDeleted = 0; + *pDeleted = Manager.DeleteAllRules(*pNotDeleted); + + UsbDkDriverAccess driver; + driver.UpdateRegistryParameters(); + + return *pNotDeleted ? InstallFailure : InstallSuccess; + } + catch (const UsbDkDriverFileException &e) + { + printExceptionString(e.what()); + return InstallSuccessNeedReboot; + } + catch (const exception &e) + { + printExceptionString(e.what()); + return InstallFailure; + } +} diff --git a/UsbDkHelper/UsbDkHelperHider.h b/UsbDkHelper/UsbDkHelperHider.h index ca05bce..ee1e7b0 100644 --- a/UsbDkHelper/UsbDkHelperHider.h +++ b/UsbDkHelper/UsbDkHelperHider.h @@ -143,7 +143,7 @@ extern "C" { * * @note * 1. Persistent rule stays until explicitly deleted by - * UsbDk_DeletePersistentHideRule() + * UsbDk_DeletePersistentHideRule or UsbDk_DeleteAllPersistentRules * 2. This API requires administrative privileges * 3. For already attached devices the rule will be applied after * device re-plug or system reboot. @@ -167,7 +167,7 @@ extern "C" { * * @note * 1. Persistent rule stays until explicitly deleted by - * UsbDk_DeletePersistentHideRule() + * UsbDk_DeleteExtendedPersistentHideRule() or UsbDk_DeleteAllPersistentRules * 2. This API requires administrative privileges * 3. For already attached devices the rule will be applied after * device re-plug or system reboot. @@ -211,6 +211,24 @@ extern "C" { */ DLL InstallResult UsbDk_DeleteExtendedPersistentHideRule(PUSB_DK_HIDE_RULE_PUBLIC Rule, ULONG Type); + /* Delete all persistent hide rules + * + * @params + * IN - None + * OUT - PULONG pDeleted - number of deleted rules + * PULONG pNotDeleted - number of not deleted rules + * + * @return + * Rule removal status + * + * @note + * 1. This API requires administrative privileges + * 2. For already attached devices the rules become inactive after + * device re-plug or system reboot. + * + */ + DLL InstallResult UsbDk_DeleteAllPersistentRules(OUT PULONG pDeleted, OUT PULONG pNotDeleted); + #ifdef __cplusplus } #endif