Simple WDF custom Context

I want to have a simple WDF custom context, lets define it as below:

struct{
void *pData;
}DEV_CONTEXT;

This is a non PnP WDF driver which handles Create, Close, Cleanup, and DeviceIocontrol.

I want this context to be available in all these functions (much like the filter manager contexts).

I have never written a WDF/KNDF driver before, so am struggling a bit here.

Questions:

  1. Where do I first allocate this context and how?
  2. How do I tuck it into the ‘Device Extension’, sorry for the WDM terminology here.
  3. How will this be available to me in the Creat/Close/DeviceIOcontrol callbacks? How do I retrieve it?
  4. When and where is this to be freed?

Read here.

Nicely describes, I think.

Peter

I think I am confused with when to use
WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE vs WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE

struct{
void *p;
} DEVEXT;

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVEXT, GetDevExt)

When I create the device I use the following:and
WDFDEVICE cd;
WDF_OBJECT_ATTRIBUTES oa
WDF_OBJECT_ATTRIBUTES_INIT(&oa);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&oa, DEVEXT);
Status = ::WdfDeviceCreate(&pDevInit, &oa, &cd);

do I also need to explicitly use WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE? If so is it before or after the WdfDeviceCreate?
Or can I start using the named Context Get function GetDevExt() directly to grab it in teh various Event routines like for Create, close etc?

To me the correct sequence seems to be

WDFDEVICE cd;
WDF_OBJECT_ATTRIBUTES oa
WDF_OBJECT_ATTRIBUTES_INIT(&oa);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&oa, DEVEXT);
WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&oa, DEVEXT)
Status = ::WdfDeviceCreate(&pDevInit, &oa, &cd);

This page doesn’t seem to mention WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE at all.

All you need is WDFDEVICE cd; WDF_OBJECT_ATTRIBUTES oa; WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&oa, DEVEXT); Status = ::WdfDeviceCreate(&pDevInit, &oa, &cd); The pattern in wdf is that you only call one init function. In this case there are overloads. INIT_CONTEXT_TYPE combines INIT and SET_CONTEXT_TYPE into one call.

BTW https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdfobject/ns-wdfobject-_wdf_object_attributes says exactly this in the 4th paragraph under Remarks.

I will throw in one more comment on this thread. Your context is a single pointer, what are you planning on allocating? I’ve encountered a number of newbie’s that don’t take things to the logical conclusion and put the data in the context structure itself. Unless you are expecting to need a varying amount of data, drop the pointer and make the context the data structure itself.

Yeah… it IS kind of confusing.

There are two alternate ways to associate a context with the device:

  1. Init the Obj Atts and then set the context:

WDF_OBJECT_ATTRIBUTES_INIT
WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE

  1. Combine them both calls above into one call, using what we call a “convenience macro”:

WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE

(I’ve never liked the fact these convenience macros, myself… for precisely the reason you have here… it’s confusing to newbies. Better to have ONE WAY to achieve the desired result. But I obviously lost that argument years ago.)

The WDF Obj Atts is an input to WdfDeviceCreate… so, this needs to be done before WdfDeviceCreate is called.

Peter

Thank you all for helping out. I have complied my sample code, and it works well.
@don : yes, this is just an experiment, I will put the context in there itself, looks like a perfect candidate for in-place new usage. This way wdf will delete it for me as well, instead of me having o clear ‘my mess’.