Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed failing to call PnPDevice::Uninstall on phantom device instance #59

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/PnP/PnPDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
/// </summary>
private readonly uint _instanceHandle;

/// <summary>
/// The <see cref="DeviceLocationFlags"/> used when creating this instance.
/// </summary>
private readonly DeviceLocationFlags _locationFlags;

/// <summary>
/// Creates a new <see cref="PnPDevice" /> based on the supplied instance ID to search in the device tree.
/// </summary>
Expand All @@ -34,6 +39,7 @@
protected unsafe PnPDevice(string instanceId, DeviceLocationFlags flags)
{
InstanceId = instanceId;
_locationFlags = flags;
uint iFlags = PInvoke.CM_LOCATE_DEVNODE_NORMAL;

switch (flags)
Expand Down Expand Up @@ -182,9 +188,9 @@
return false;
}

string parentId = device.GetProperty<string>(DevicePropertyKey.Device_Parent);

Check warning on line 191 in src/PnP/PnPDevice.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

if (parentId.Equals(@"HTREE\ROOT\0", StringComparison.OrdinalIgnoreCase))

Check warning on line 193 in src/PnP/PnPDevice.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
break;
}
Expand Down Expand Up @@ -538,11 +544,18 @@
SetupApi.SP_DRVINFO_DATA driverInfoData = new();
driverInfoData.cbSize = Marshal.SizeOf(driverInfoData);

uint iFlags = PInvoke.DIGCF_ALLCLASSES;

if (_locationFlags.HasFlag(DeviceLocationFlags.Normal))
{
iFlags |= PInvoke.DIGCF_PRESENT;
}

HDEVINFO hDevInfo = SetupApi.SetupDiGetClassDevs(
null,
null,
HWND.Null,
PInvoke.DIGCF_ALLCLASSES | PInvoke.DIGCF_PRESENT
iFlags
);

if (hDevInfo.IsNull)
Expand Down
Loading