Книга: Microsoft Windows Embedded CE 6.0 Exam Preparation Kit

Device Driver Context

Device Driver Context

Device Manager supports context management based on device context and open context parameters, which Device Manager passes as DWORD values to the stream driver with each function call. Context management is vital if a driver must allocate and deallocate instance-specific resources, such as blocks of memory. It is important to keep in mind that device drivers are DLLs, which implies that global variables and other memory structures defined or allocated in the driver are shared by all driver instances. Deallocating the wrong resources in response to an XXX_Close or XXX_Deinit call can lead to memory leaks, application failures, and general system instabilities.Stream drivers can manage context information per device driver instance based on the following two levels:

1. Device context The driver initializes this context in the XXX_Init function. This context is therefore also called the Init Context. Its primary purpose is to help the driver manage resources related to hardware access. Device Manager passes this context information to the XXX_Init, XXX_Open, XXX_PowerUp, XXX_PowerDown, XXX_PreDeinit and XXX_Deinit functions.

2. Open context The driver initializes this second context in the XXX_Open function. Each time an application calls CreateFile for a stream driver, the stream driver creates a new open context. The open context then enables the stream driver to associate data pointers and other resources with each opened driver instance. Device Manager passes the device context to the stream driver in the XXX_Open function so that the driver can store a reference to the device context in the open context. In this way, the driver can retain access to the device context information in subsequent calls, such as XXX_Read, XXX_Write, XXX_Seek, XXX_IOControl, XXX_PreClose and XXX_Close. Device Manager only passes the open context to these functions in the form of a DWORD parameter.

The following code listing illustrates how to initialize a device context for a sample driver with the driver name SMP (such as SMP1:):

DWORD SMP_Init(LPCTSTR pContext, LPCVOID lpvBusContext) {
 T_DRIVERINIT_STRUCTURE *pDeviceContext = (T_DRIVERINIT_STRUCTURE *)
  LocalAlloc(LMEM_ZEROINIT|LMEM_FIXED, sizeof(T_DRIVERINIT_STRUCTURE));
 if (pDeviceContext == NULL) {
  DEBUGMSG(ZONE_ERROR,(L" SMP: ERROR: Cannot allocate memory "
   + "for sample driver's device context.rn"));
  // Return 0 if the driver failed to initialize.
  return 0;
 }
 // Perform system intialization...
 pDeviceContext->dwOpenCount = 0;
 DEBUGMSG(ZONE_INIT,(L"SMP: Sample driver initialized.rn"));
 return (DWORD)pDeviceContext;
}

Оглавление книги


Генерация: 0.998. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз