How to Retrieve ETHREAD object for KTHREAD object

Hi,

I am working on an audio miniport driver for Windows 10, in which i am calling the function AddStreamResource() after retrieving the interface IID_IPortClsStreamResourceManager.

This function accepts the resources that are owned by the driver and gives it to PortCls (or OS) which manages these resources for a glitch free operation.

This function accepts a parameter of type PPCSTREAMRESOURCE_DESCRIPTOR which is a structure that has PETHREAD member. This member is intended to hold the threads that are owned by the driver.

however for the thread that i am creating in the driver using PsCreateSystemThread() i am able to get KTHREAD object by using ObReferenceObjectByHandle. Is there any way i can retrieve the ETHREAD object from this KTHREAD Object so that i could store it in PPCSTREAMRESOURCE_DESCRIPTOR structure, and pass it to AddStreamResource() function??

You can see in the MSDN documentation for ObReferenceObjectByHandle (
https://msdn.microsoft.com/en-us/library/windows/hardware/ff558679(v=vs.85).aspx)
the following fact:
“The structures that the pointer types reference are opaque, and drivers
cannot access the structure members. Because the structures are opaque,
PEPROCESS is equivalent to PKPROCESS, and *PETHREAD is equivalent to
PKTHREAD*.”

Also, if you would have taken a look in WinDbg at the ETHREAD structure you
would have seen that its first member is a KTHREAD structure.

On 1 October 2015 at 12:37, wrote:

> Hi,
>
> I am working on an audio miniport driver for Windows 10, in which i am
> calling the function AddStreamResource() after retrieving the interface
> IID_IPortClsStreamResourceManager.
>
> This function accepts the resources that are owned by the driver and gives
> it to PortCls (or OS) which manages these resources for a glitch free
> operation.
>
> This function accepts a parameter of type PPCSTREAMRESOURCE_DESCRIPTOR
> which is a structure that has PETHREAD member. This member is intended to
> hold the threads that are owned by the driver.
>
> however for the thread that i am creating in the driver using
> PsCreateSystemThread() i am able to get KTHREAD object by using
> ObReferenceObjectByHandle. Is there any way i can retrieve the ETHREAD
> object from this KTHREAD Object so that i could store it in
> PPCSTREAMRESOURCE_DESCRIPTOR structure, and pass it to AddStreamResource()
> function??
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> 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
>

pointer to PKTHREAD and PETHREAD is equal. really ETHREAD is containing KTHREAD at very begin or struct ETHREAD : KTHREAD in c++ notation. so you can use simply cast from one type pointer to another

When i try to pass PKTHREAD to PETHREAD, code doesn’t compile. When i try
to type cast the PKTRHEAD to PETHREAD,

PETHREAD Thread = (PETHREAD)m_Thread;

I am seeing a crash access violation
SYSTEM_THREAD_EXCEPTION_NOT_HANDLED. Is it o.K to typecast like this
before passing to OS calls?

J.S.R.Sarma.
9916109893.

On Thu, Oct 1, 2015 at 3:16 PM, wrote:

> pointer to PKTHREAD and PETHREAD is equal. really ETHREAD is containing
> KTHREAD at very begin or struct ETHREAD : KTHREAD in c++ notation. so you
> can use simply cast from one type pointer to another
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> 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
>

>When i try to pass PKTHREAD to PETHREAD, code doesn’t compile. -of course? need type cast

PETHREAD Thread = (PETHREAD)m_Thread;
Is it o.K to typecast like this
yes,it is ok.
SYSTEM_THREAD_EXCEPTION_NOT_HANDLED. - this is by some another reason

As Mr. Brown said, it is absolutely safe… and even accepted best practice… to freely cast PETHREAD to PKTHREAD and PEPROCESS to PKPROCESS.

And just to note…

PETHREAD Thread = (PETHREAD)m_Thread;

can’t cause an exception. You’re simply assigning a value to a local (stack based) variable.

Peter
OSR
@OSRDrivers

> it is absolutely safe… and even accepted best practice… to freely cast PETHREAD

to PKTHREAD and PEPROCESS to PKPROCESS.

What a disappointment for me - I was waiting for our “usual suspects” to jump in and tell us that it won’t work, request the product name so that they would advise their clients " to stay away from this crap" (apparently, all the above would be done in capitals), and, in general, do all other “funny things” that they normally do in situations like that. However, once you said it is perfectly fine they are obviously not going to do it. What a pity…

Anton Bassov