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.
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.
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.
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’.