From 6f898b8053b8172f50a1351029fa97310e3f9eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Fri, 22 May 2020 21:57:43 +0200 Subject: [PATCH] Added duplicate device check --- sys/Driver.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/sys/Driver.cpp b/sys/Driver.cpp index ac2e697..6d42c2f 100644 --- a/sys/Driver.cpp +++ b/sys/Driver.cpp @@ -114,6 +114,7 @@ NTSTATUS Bus_EvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit) WDF_OBJECT_ATTRIBUTES fdoAttributes; WDF_OBJECT_ATTRIBUTES fileHandleAttributes; PFDO_DEVICE_DATA pFDOData; + PWSTR pSymbolicNameList; UNREFERENCED_PARAMETER(Driver); @@ -121,6 +122,46 @@ NTSTATUS Bus_EvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit) TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); +#pragma region Check for duplicated FDO + + // + // Note: this could be avoided if converted to non-PNP driver + // and use of named device object. Food for thought for future. + // + + status = IoGetDeviceInterfaces( + &GUID_DEVINTERFACE_BUSENUM_VIGEM, + NULL, + 0, // Important! + &pSymbolicNameList + ); + if (NT_SUCCESS(status)) + { + const bool deviceAlreadyExists = (0 != *pSymbolicNameList); + ExFreePool(pSymbolicNameList); + + if (deviceAlreadyExists) + { + TraceEvents(TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "Device with interface GUID {%!GUID!} already exists (%ws)", + &GUID_DEVINTERFACE_BUSENUM_VIGEM, + pSymbolicNameList + ); + + return STATUS_RESOURCE_IN_USE; + } + } + else + { + TraceEvents(TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "IoGetDeviceInterfaces failed with status %!STATUS!", + status); + } + +#pragma endregion + WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_BUS_EXTENDER); // More than one process may talk to the bus at the same time WdfDeviceInitSetExclusive(DeviceInit, FALSE);