Hi Doron,
My WDFDEVICE is a filter.
I have added following code in filter device add.
status = FilterCreateControlDevice(device);
my FilterCreateControlDevice method contains the following code
Use_decl_annotations
NTSTATUS
FilterCreateControlDevice(
WDFDEVICE Device
)
{
PWDFDEVICE_INIT pInit = NULL;
WDFDEVICE controlDevice = NULL;
WDF_OBJECT_ATTRIBUTES controlAttributes;
WDF_IO_QUEUE_CONFIG ioQueueConfig;
BOOLEAN bCreate = FALSE;
NTSTATUS status;
WDFQUEUE queue;
DECLARE_CONST_UNICODE_STRING(ntDeviceName, NTDEVICE_NAME_STRING);
DECLARE_CONST_UNICODE_STRING(symbolicLinkName, SYMBOLIC_NAME_STRING);
PAGED_CODE();
//
// First find out whether any ControlDevice has been created. If the
// collection has more than one device then we know somebody has already
// created or in the process of creating the device.
//
WdfWaitLockAcquire(FilterDeviceCollectionLock, NULL);
if (WdfCollectionGetCount(FilterDeviceCollection) == 1) {
bCreate = TRUE;
}
WdfWaitLockRelease(FilterDeviceCollectionLock);
if (!bCreate) {
//
// Control device is already created. So return success.
//
return STATUS_SUCCESS;
}
KdPrint((“Creating Control Device\n”));
//
//
// In order to create a control device, we first need to allocate a
// WDFDEVICE_INIT structure and set all properties.
//
pInit = WdfControlDeviceInitAllocate(
WdfDeviceGetDriver(Device),
&SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_RW_RES_R
//&SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_RWX_RES_RWX
);
if (pInit == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto Error;
}
//
// Set exclusive to false so that more than one app can talk to the
// control device simultaneously.
//
WdfDeviceInitSetExclusive(pInit, FALSE);
status = WdfDeviceInitAssignName(pInit, &ntDeviceName);
if (!NT_SUCCESS(status)) {
goto Error;
}
//
// Specify the size of device context
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&controlAttributes,
CONTROL_DEVICE_EXTENSION);
status = WdfDeviceCreate(&pInit,
&controlAttributes,
&controlDevice);
if (!NT_SUCCESS(status)) {
goto Error;
}
//
// Create a symbolic link for the control object so that usermode can open
// the device.
//
status = WdfDeviceCreateSymbolicLink(controlDevice,
&symbolicLinkName);
if (!NT_SUCCESS(status)) {
goto Error;
}
//
// Configure the default queue associated with the control device object
// to be Serial so that request passed to EvtIoDeviceControl are serialized.
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQueueConfig,
WdfIoQueueDispatchSequential);
ioQueueConfig.EvtIoDeviceControl = FilterEvtIoDeviceControl;
//
// Framework by default creates non-power managed queues for
// filter drivers.
//
status = WdfIoQueueCreate(controlDevice,
&ioQueueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&queue // pointer to default queue
);
if (!NT_SUCCESS(status)) {
goto Error;
}
//
// Control devices must notify WDF when they are done initializing. I/O is
// rejected until this call is made.
//
WdfControlFinishInitializing(controlDevice);
ControlDevice = controlDevice;
return STATUS_SUCCESS;
Error:
if (pInit != NULL) {
WdfDeviceInitFree(pInit);
}
if (controlDevice != NULL) {
//
// Release the reference on the newly created object, since
// we couldn’t initialize it.
//
WdfObjectDelete(controlDevice);
controlDevice = NULL;
}
return status;
}
Is this the right way to create the control device ?
BR,
Dhaval