PsCreateSystemThread in KMDF

Hi,
I am developing a USB KMDF driver. I have a scenario where i have to create system worker thread. i have a PsCreateSystemThread function in my EvtIoDeviceControl callback. My call function looks like this

istatus =
PsCreateSystemThread( pthreadhandle,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
func,
&mydevice
);
i am passing address of WDFDEVICE object to this thread procedure as paramater.

My precedure is like this

void func(IN WDFDEVICE* device)
{
NFCSTATUS infcstatus;
phNfc4Win_PollState_t ThreadState = NODEVICE;
phNfc4Win_sRemoteDevInfo_t RemDevInfo[2];
uint8_t pNbrOfRemoteDev = 2;
WDFDEVICE myDevice;
phNfc4Win_sDevInputParam_t puDevInputParam;
NRB_DISCOVERY_PARAMETER iDiscPar;
PDEVICE_CONTEXT pDevContext;

DbgPrint(“\n--------THREAD CREATED----------\n”);

pDevContext = GetDeviceContext(*device);
// Device = *(mainctx->psDevContxt->pdevice);
//
//
//
//

}

As soon as i access the device object in thread, iam getting a crash.
So, wat can i do about this.

  1. am I creating thread properly,
  2. can WDF object be accessed in the context of the created thread
  3. Am i doing some small mistake which iam not able to see in the above code

regards,
king

My guess would be that “mydevice” is a local (ie stack address) variable when you create the second thread. When the second thread starts, the routine has since exited and it contains garbage, leading to an access violation and one sort of bugcheck or another.

Easy fix- pass the WDFDEVICE handle itself, not the address of a variable containing the device handle. If for some reason you simply must have a variable then allocate it from pool and figure out how you’re going to manage it (or use a global and risk eventually finding you’ve painted yourself into a corner).

You are passing the address of device. The adress of a local or parameter is stack based, so you are corrupting some other stack potentially when you dereference it.

The fix is to pass device by value.

D

-----Original Message-----
From: “xxxxx@yahoo.com
To: “Windows System Software Devs Interest List”
Sent: 07/06/07 4:10 AM
Subject: [ntdev] PsCreateSystemThread in KMDF

Hi,
I am developing a USB KMDF driver. I have a scenario where i have to create system worker thread. i have a PsCreateSystemThread function in my EvtIoDeviceControl callback. My call function looks like this

istatus =
PsCreateSystemThread( pthreadhandle,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
func,
&mydevice
);
i am passing address of WDFDEVICE object to this thread procedure as paramater.

My precedure is like this

void func(IN WDFDEVICE* device)
{
NFCSTATUS infcstatus;
phNfc4Win_PollState_t ThreadState = NODEVICE;
phNfc4Win_sRemoteDevInfo_t RemDevInfo[2];
uint8_t pNbrOfRemoteDev = 2;
WDFDEVICE myDevice;
phNfc4Win_sDevInputParam_t puDevInputParam;
NRB_DISCOVERY_PARAMETER iDiscPar;
PDEVICE_CONTEXT pDevContext;

DbgPrint(“\n--------THREAD CREATED----------\n”);

pDevContext = GetDeviceContext(*device);
// Device = *(mainctx->psDevContxt->pdevice);
//
//
//
//

}

As soon as i access the device object in thread, iam getting a crash.
So, wat can i do about this.

1. am I creating thread properly,
2. can WDF object be accessed in the context of the created thread
3. Am i doing some small mistake which iam not able to see in the above code

regards,
king


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

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