Microsoft KB Archive/298864

= INFO: TLS Considerations for Device Drivers =

Article ID: 298864

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Windows CE 3.0 for the Handheld PC
 * Microsoft Windows CE 2.12 for the Handheld PC
 * Microsoft Windows CE 2.11 for the Handheld PC
 * Microsoft Windows CE 2.0 for the Handheld PC
 * Microsoft Windows CE Platform Builder 2.11
 * Microsoft Windows CE Platform Builder 2.12
 * Microsoft Encarta Reference Suite 2001

-



This article was previously published under Q298864



SUMMARY
A device driver implementation must take into account whether its thread of execution is owned by a calling process (application) or its own process. Those that are owned by a calling process must not make system calls that will potentially access thread local storage (TLS).



MORE INFORMATION
In Windows CE, an application thread can make a number of system calls that cause the thread to transition from the application's process space to the driver's process space. An example of this would be a display driver getting called as a result of an ExtEscape or a number of graphics device interface (GDI) and window manager functions. The display driver runs in the GWES (Gwes.exe) process space.

Because TLS slots are reserved per process in a structure owned by the thread, the driver cannot use the TLS slots of an application's thread. Some system API’s (such as gethostbyname) will attempt to use these TLS slots and should not be called while on an application thread within a device driver. This applies to drivers loaded by GWES (such as display, keyboard, and touch panel) and all drivers loaded by Device Manager (Device.exe).

A driver can compare the results of GetOwnerProcess and OpenProcess to determine if the thread is owned by the current process or by an application. HANDLE hOwnerProcess; HANDLE hDriverProcess; BOOL fIsApplicationThread;

hOwnerProcess = GetOwnerProcess; hDriverProcess = OpenProcess( 0, FALSE, GetCurrentProcessID ); fIsApplicationThread = ( hOwnerProcess != hDriverProcess ); If it is a requirement of the driver's architecture to use functions like gethostbyname on behalf of the caller, the driver will have to create a secondary thread to make the call.

