Calling kmdf functions from a dll causes bugcheck

I am trying to write a library that’ll link to a driver, both using kmdf.
When I build the library as a static library and statically link it to the driver, it works fine.
However, when I build & link it as a dll (EXPORT_DRIVER), I get a bugcheck as soon as the dll calls any kmdf function.

Bug check is SYSTEM_THREAD_EXCEPTION_NOT_HANDLED, for exception c0000005.

Stack trace shows that the driver calls the dll, which in turn calls WdfObjectCreate, and then 0x0.

Faulting source code is:

WDFOBJECT* Object
561: )
562: {
563: return ((PFN_WDFOBJECTCREATE) WdfFunctions[WdfObjectCreateTableIndex])(WdfDriverGlobals, Attributes, Object);

564: }

WdfObjectCreate, according to the stack trace, is implemented at the dll image. I suppose that WdfFunctions is a vector in my dll that should be somehow initialized, but how?

KMDF is not supported for DLLs, there is no loader support to bind the DLL to a runtime version of KMDF.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@yahoo.com
Sent: Tuesday, September 22, 2009 4:08 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Calling kmdf functions from a dll causes bugcheck

I am trying to write a library that’ll link to a driver, both using kmdf.
When I build the library as a static library and statically link it to the driver, it works fine.
However, when I build & link it as a dll (EXPORT_DRIVER), I get a bugcheck as soon as the dll calls any kmdf function.

Bug check is SYSTEM_THREAD_EXCEPTION_NOT_HANDLED, for exception c0000005.

Stack trace shows that the driver calls the dll, which in turn calls WdfObjectCreate, and then 0x0.

Faulting source code is:

WDFOBJECT* Object
561: )
562: {
563: return ((PFN_WDFOBJECTCREATE) WdfFunctions[WdfObjectCreateTableIndex])(WdfDriverGlobals, Attributes, Object);

564: }

WdfObjectCreate, according to the stack trace, is implemented at the dll image. I suppose that WdfFunctions is a vector in my dll that should be somehow initialized, but how?


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Any way to copy the runtime information from the driver to the dll (assuming they are tightly coupled and both use the same version of the framework)?

Here’s what I’ve done:

I added an init function to the dll, that does this:

DLL_API VOID DllInit(WDFDRIVER Driver,WDFFUNC *WdfFunc)
{
WdfDriver=Driver;

RtlCopyMemory(WdfFunctions,WdfFunc,396*sizeof(WDFFUNC));
}

It stores the driver handle for later use, and then copies the entire WdfFunc vector to the local vector. 396 is taken from looking at wdffuncenum.h.
The driver calls this function after it creates the driver object, and passes the handle and it’s own copy of WdfFunctions.

Currently it seems to do the trick fine, but I haven’t stress-tested it. Any suggestions of yours as to how stable it is? (Again, assuming that both the drvier and dll using the same framework version)

* 396 should actually be 397

Not possible

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: xxxxx@yahoo.com
Sent: Tuesday, September 22, 2009 4:49 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Calling kmdf functions from a dll causes bugcheck

Any way to copy the runtime information from the driver to the dll (assuming they are tightly coupled and both use the same version of the framework)?


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

This might appear to work, but it will blow up later. There are other private globals that need to be initialized, the kmdf runtime does not know about the module so it can’t track lifetime and provide rundown protection, you cannot share contexts on wdf objects. That is just the start.

Do not ship code like this

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: xxxxx@yahoo.com
Sent: Tuesday, September 22, 2009 5:25 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Calling kmdf functions from a dll causes bugcheck

Here’s what I’ve done:

I added an init function to the dll, that does this:

DLL_API VOID DllInit(WDFDRIVER Driver,WDFFUNC WdfFunc)
{
WdfDriver=Driver;

RtlCopyMemory(WdfFunctions,WdfFunc,396
sizeof(WDFFUNC));
}

It stores the driver handle for later use, and then copies the entire WdfFunc vector to the local vector. 396 is taken from looking at wdffuncenum.h.
The driver calls this function after it creates the driver object, and passes the handle and it’s own copy of WdfFunctions.

Currently it seems to do the trick fine, but I haven’t stress-tested it. Any suggestions of yours as to how stable it is? (Again, assuming that both the drvier and dll using the same framework version)


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

> I added an init function to the dll, that does this:

I would make the interface between the main driver and the DLL absolutely KMDF-agnostic, so that the DLL will know nothing on KMDF.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

BTW, is it ok to use kmdf (including driver-library kmdf object passing) in a static library?

Yes a static lib is fine b/c it ends up being on image (the sys file)

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: xxxxx@yahoo.com
Sent: Wednesday, September 23, 2009 5:09 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Calling kmdf functions from a dll causes bugcheck

BTW, is it ok to use kmdf (including driver-library kmdf object passing) in a static library?


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Yes, why not?


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

wrote in message news:xxxxx@ntdev…
> BTW, is it ok to use kmdf (including driver-library kmdf object passing) in a static library?
>