DRIVER CONTEXT AND SHARED MEMORY
The stream Init function is called each time the driver is loaded. The global data in the DLL is specific to each instance of the driver. For example if
is included below the header file included in the driver source file, then when Init runs, the count is always zero. How can driver instances know how many instances have loaded? This may be of importance for determining what hardware to access in the driver, or for limiting the number of instances to one if there is only one set of relevant hardware available.
DLLs can implement shared memory in two ways. The simplest uses named data segments. The second more complex method uses memory mapped files. The data segments approach is suitable for simple data such as a counter but not for structured data and pointers.
The following code implements the device counter (devCount) that can be incremented in each Init function and decremented in each Deinit function.
1. Place the first three lines of code in the global part of the driver DLL code.
2. Place the two lines from SECTIONS at the bottom of the driver’s .def file. (Remember it may not show in the driver project although it is used in the build. Explore the driver’s project directory to access it.)
3. Also add the Init and Deinit code to those functions.
#pragma data_seg(".MYSEG") static DWORD devCount=0; #pragma data_seg() SECTIONS ...