Skip to content

Add Remove Ghost Devices action - #514

Open
PrimeBuild-pc wants to merge 2 commits into
lostindark:masterfrom
PrimeBuild-pc:feat/remove-ghost-devices
Open

PrimeBuild-pc wants to merge 2 commits into
lostindark:masterfrom
PrimeBuild-pc:feat/remove-ghost-devices

Conversation

@PrimeBuild-pc

Copy link
Copy Markdown

Refs #345

I know you pointed to Uwe Sieber's Device Cleanup Tool in that issue, and it is a good tool. I still put together a small built-in version because people looking at unused packages in DSE are usually the same people who want to clear the stale devices that keep those packages "in use", and doing both in one place saves a separate download. If you'd rather not take it, no problem at all.

What it does

File > Remove Ghost Devices... scans for non-present devices, shows them in a dialog with checkboxes (device, class, instance ID), and uninstalls the checked ones. Afterwards the driver list is refreshed, so packages that only served the removed devices show up as unused.

Remove Ghost Devices dialog

Implementation

  • Reuses the existing CfgMgr32 interop in ConfigManager: same device ID enumeration, CM_Locate_DevNode(PHANTOM) and CM_Get_DevNode_Status. A device counts as a ghost only when the status call returns CR_NO_SUCH_DEVNODE; any other error is treated as present.
  • Removal uses CM_Uninstall_DevNode (one new P/Invoke). No pnputil, no new NuGet package.
  • GhostDevicesForm is built in code, like the details pane and the newer menu items, since its strings have no generated Language properties yet. New strings are only in the neutral Language.resx.

Safety

  • Presence is checked again right before each removal; a device that was plugged in after the scan is skipped and reported as failed.
  • Software-enumerated devices (ROOT\, SWD\, HTREE\, SW\, UMB\, {GUID}\ buses) are listed but not checked by default.
  • Explicit confirmation (default button is Cancel), then a summary that lists any devices that could not be removed. Each removal is logged.
  • Disabled for offline driver stores and while another operation runs. The app already requires admin.

Testing

  • Release build: 0 errors, 0 warnings. dotnet test: 204 passed, including new tests for the ghost / pre-selection rules.
  • On my machine (read-only): the scan returns the same 169 devices as Get-PnpDevice | ? { -not $_.Present }.
  • In a disposable Hyper-V Windows 11 VM: 87 ghosts found (matching Get-PnpDevice); removing the 83 pre-selected ones succeeded 83/83, they no longer appear, the present device count stayed 63/63, and a call on a present device was refused and left it untouched. The VM was then reverted to its checkpoint.
  • The screenshot uses sample entries from that VM. I did not click through the full menu flow in the VM UI, only the dialog and the scan/removal code paths.

Lists non-present devices (CfgMgr32, CM_Get_DevNode_Status returning
CR_NO_SUCH_DEVNODE) in a checkbox dialog and uninstalls the selected ones
with CM_Uninstall_DevNode. Presence is checked again right before removal,
software-enumerated devices (ROOT, SWD, HTREE, SW, UMB, GUID buses) are not
pre-selected, and the action is disabled for offline driver stores. The
driver list is refreshed afterwards so packages left unused show up.

Refs lostindark#345

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests do not exercise the removal workflow, leaving the last-moment presence guard against uninstalling reconnected devices and uninstall failure handling unverified.

1 open issue
What changed in this PR

Adds built-in ghost-device cleanup to complement driver-package management, addressing #345 without requiring a separate cleanup tool.

Changes:

  • Adds an online-only File > Remove Ghost Devices action with device selection, confirmation, and result reporting.
  • Rechecks device presence before removal and refreshes the driver list afterward.
  • Adds resource strings and tests for ghost classification and default-selection rules.
File Description
RaprTests/​Utils/​GhostDeviceTests.cs Tests ghost classification and software-device selection exclusions.
Rapr/​Utils/​GhostDevice.cs Defines ghost-device metadata and selection rules.
Rapr/​Utils/​ConfigManager.cs Adds ghost enumeration and presence-checked native removal.
Rapr/​Lang/​Language.resx Adds English menu, dialog, confirmation, and status strings.
Rapr/​GhostDevicesForm.cs Adds the checkbox-based device-selection dialog.
Rapr/​DSEForm.cs Integrates the menu action, operation controls, and post-removal refresh.

Review effort: Balanced

Comment thread Rapr/Utils/ConfigManager.cs Outdated
if (result == ConfigManagerResult.Success)
{
// Check again: the device may have been connected since the scan.
if (!GhostDevice.IsGhost(IsDevicePresent(devInst)))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8873bbb. TryRemoveGhostDevice now takes the locate, status and uninstall calls as delegates (same pattern as GetDeviceIds / ReadDevNodeProperty). New data-driven test covers: absent device is uninstalled; uninstall failure returns false; present, unknown status, and locate failures never call uninstall.

TryRemoveGhostDevice now takes the locate, status and uninstall calls as
delegates, like GetDeviceIds and ReadDevNodeProperty, so tests can check
that present, unknown or unlocatable devices are never uninstalled and
that uninstall failures are reported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants