NTSTATUS DeviceGetPdoName( _In_ PDEVICE_CONTEXT DeviceContext ) { NTSTATUS status; WDFDEVICE device = DeviceContext->Device; WDF_OBJECT_ATTRIBUTES attributes; WDFMEMORY memory; WDF_OBJECT_ATTRIBUTES_INIT(&attributes); attributes.ParentObject = device; status = WdfDeviceAllocAndQueryProperty( device, DevicePropertyPhysicalDeviceObjectName, NonPagedPool, &attributes, &memory); if (!NT_SUCCESS(status)) { Trace(TRACE_LEVEL_ERROR, "Error: Failed to query PDO name"); goto Exit; } DeviceContext->PdoName = (PWCHAR) WdfMemoryGetBuffer(memory, NULL); Trace(TRACE_LEVEL_ERROR, "PDO Name is %ws", DeviceContext->PdoName); Exit: return status; }
VOID GetDeviceEventLoggingNames( _In_ WDFDEVICE Device ) /*++ Routine Description: Retrieve the friendly name and the location string into WDFMEMORY objects and store them in the device context. Arguments: Return Value: None --*/ { PDEVICE_CONTEXT pDevContext = GetDeviceContext(Device); WDF_OBJECT_ATTRIBUTES objectAttributes; WDFMEMORY deviceNameMemory = NULL; WDFMEMORY locationMemory = NULL; NTSTATUS status; PAGED_CODE(); // // We want both memory objects to be children of the device so they will // be deleted automatically when the device is removed. // WDF_OBJECT_ATTRIBUTES_INIT(&objectAttributes); objectAttributes.ParentObject = Device; // // First get the length of the string. If the FriendlyName // is not there then get the lenght of device description. // status = WdfDeviceAllocAndQueryProperty(Device, DevicePropertyFriendlyName, NonPagedPoolNx, &objectAttributes, &deviceNameMemory); if (!NT_SUCCESS(status)) { status = WdfDeviceAllocAndQueryProperty(Device, DevicePropertyDeviceDescription, NonPagedPoolNx, &objectAttributes, &deviceNameMemory); } if (NT_SUCCESS(status)) { pDevContext->DeviceNameMemory = deviceNameMemory; pDevContext->DeviceName = WdfMemoryGetBuffer(deviceNameMemory, NULL); } else { pDevContext->DeviceNameMemory = NULL; pDevContext->DeviceName = L"(error retrieving name)"; } // // Retrieve the device location string. // status = WdfDeviceAllocAndQueryProperty(Device, DevicePropertyLocationInformation, NonPagedPoolNx, WDF_NO_OBJECT_ATTRIBUTES, &locationMemory); if (NT_SUCCESS(status)) { pDevContext->LocationMemory = locationMemory; pDevContext->Location = WdfMemoryGetBuffer(locationMemory, NULL); } else { pDevContext->LocationMemory = NULL; pDevContext->Location = L"(error retrieving location)"; } return; }
NTSTATUS FireFlyEvtDeviceAdd( WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit ) /*++ Routine Description: EvtDeviceAdd is called by the framework in response to AddDevice call from the PnP manager. We create and initialize a device object to represent to be part of the device stack as a filter. Arguments: Driver - Handle to a framework driver object created in DriverEntry DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure. Return Value: NTSTATUS --*/ { WDF_OBJECT_ATTRIBUTES attributes; NTSTATUS status; PDEVICE_CONTEXT pDeviceContext; WDFDEVICE device; WDFMEMORY memory; size_t bufferLength; UNREFERENCED_PARAMETER(Driver); PAGED_CODE(); // // Configure the device as a filter driver // WdfFdoInitSetFilter(DeviceInit); WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT); status = WdfDeviceCreate(&DeviceInit, &attributes, &device); if (!NT_SUCCESS(status)) { KdPrint(("FireFly: WdfDeviceCreate, Error %x\n", status)); return status; } // // Driver Framework always zero initializes an objects context memory // pDeviceContext = WdfObjectGet_DEVICE_CONTEXT(device); // // Initialize our WMI support // status = WmiInitialize(device, pDeviceContext); if (!NT_SUCCESS(status)) { KdPrint(("FireFly: Error initializing WMI 0x%x\n", status)); return status; } // // In order to send ioctls to our PDO, we have open to open it // by name so that we have a valid filehandle (fileobject). // When we send ioctls using the IoTarget, framework automatically // sets the filobject in the stack location. // WDF_OBJECT_ATTRIBUTES_INIT(&attributes); // // By parenting it to device, we don't have to worry about // deleting explicitly. It will be deleted along witht the device. // attributes.ParentObject = device; status = WdfDeviceAllocAndQueryProperty(device, DevicePropertyPhysicalDeviceObjectName, NonPagedPoolNx, &attributes, &memory); if (!NT_SUCCESS(status)) { KdPrint(("FireFly: WdfDeviceAllocAndQueryProperty failed 0x%x\n", status)); return STATUS_UNSUCCESSFUL; } pDeviceContext->PdoName.Buffer = WdfMemoryGetBuffer(memory, &bufferLength); if (pDeviceContext->PdoName.Buffer == NULL) { return STATUS_UNSUCCESSFUL; } pDeviceContext->PdoName.MaximumLength = (USHORT) bufferLength; pDeviceContext->PdoName.Length = (USHORT) bufferLength-sizeof(UNICODE_NULL); return status; }
NTSTATUS FmCreateDosDevicesSymbolicLink( WDFDEVICE Device, PFM_DEVICE_DATA FmDeviceData ) { NTSTATUS status; UNICODE_STRING comPort; UNICODE_STRING pdoName; UNICODE_STRING symbolicLink; WDFKEY hKey = NULL; DECLARE_CONST_UNICODE_STRING(valueName, L"PortName"); WDFSTRING string = NULL; WDFMEMORY memory; WDF_OBJECT_ATTRIBUTES memoryAttributes; size_t bufferLength; PAGED_CODE(); symbolicLink.Buffer = NULL; // // Open the device registry and read the "PortName" value written by the // class installer. // status = WdfDeviceOpenRegistryKey(Device, PLUGPLAY_REGKEY_DEVICE, STANDARD_RIGHTS_ALL, NULL, // PWDF_OBJECT_ATTRIBUTES &hKey); if (!NT_SUCCESS (status)) { goto Error; } status = WdfStringCreate( NULL, WDF_NO_OBJECT_ATTRIBUTES , &string ); if (!NT_SUCCESS(status)) { goto Error; } // // Retrieve the value of ValueName from registry // status = WdfRegistryQueryString( hKey, &valueName, string ); if (!NT_SUCCESS (status)) { goto Error; } // // Retrieve the UNICODE_STRING from string object // WdfStringGetUnicodeString( string, &comPort ); WdfRegistryClose(hKey); hKey = NULL; symbolicLink.Length=0; symbolicLink.MaximumLength = sizeof(OBJECT_DIRECTORY) + comPort.MaximumLength; symbolicLink.Buffer = ExAllocatePoolWithTag(PagedPool, symbolicLink.MaximumLength + sizeof(WCHAR), 'wkaF'); if (symbolicLink.Buffer == NULL) { status = STATUS_INSUFFICIENT_RESOURCES; goto Error; } RtlZeroMemory(symbolicLink.Buffer, symbolicLink.MaximumLength); RtlAppendUnicodeToString(&symbolicLink, OBJECT_DIRECTORY); RtlAppendUnicodeStringToString(&symbolicLink, &comPort); // // This DDI will get the underlying PDO name and create a symbolic to that // because our FDO doesn't have a name. // status = WdfDeviceCreateSymbolicLink(Device, &symbolicLink); if (!NT_SUCCESS(status)) { goto Error; } WDF_OBJECT_ATTRIBUTES_INIT(&memoryAttributes); memoryAttributes.ParentObject = Device; status = WdfDeviceAllocAndQueryProperty(Device, DevicePropertyPhysicalDeviceObjectName, PagedPool, &memoryAttributes, &memory); if (!NT_SUCCESS(status)) { // // We expect a zero length buffer. Anything else is fatal. // goto Error; } pdoName.Buffer = WdfMemoryGetBuffer(memory, &bufferLength); if (pdoName.Buffer == NULL) { status = STATUS_INSUFFICIENT_RESOURCES; goto Error; } pdoName.MaximumLength = (USHORT) bufferLength; pdoName.Length = (USHORT) bufferLength - sizeof(UNICODE_NULL); status = RtlWriteRegistryValue(RTL_REGISTRY_DEVICEMAP, L"SERIALCOMM", pdoName.Buffer, REG_SZ, comPort.Buffer, comPort.Length); if (!NT_SUCCESS(status)) { goto Error; } FmDeviceData->Flags |= REG_VALUE_CREATED_FLAG; // // Store it so it can be deleted later. // FmDeviceData->PdoName = pdoName; Error: if (symbolicLink.Buffer != NULL) { ExFreePool(symbolicLink.Buffer); } if (hKey != NULL) { WdfRegistryClose(hKey); } if (string != NULL) { WdfObjectDelete(string); } return status; }