Microsoft KB Archive/194945

{|
 * width="100%"|

SAMPLE: Section.exe on Sharing Memory Between Kernel & User Mode

 * }

Q194945

-

The information in this article applies to:


 * Microsoft Win32 Device Driver Kit (DDK) for Windows NT, version 4.0

-

SUMMARY
Section.exe is a sample driver that demonstrates how to share memory between user-mode and kernel-mode by using section objects (memory-mapped file). There are other better and more efficient methods available to share memory. Please see the following article in the Microsoft Knowledge Base before using this method:

"Q191840 HOWTO: Share Memory Between User Mode and Kernel Mode"

MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

"Section.exe" Release Date: Oct-29-1998

For additional information about how to download Microsoft Support files, click the article number below to view the article in the Microsoft Knowledge Base:

"Q119591 How to Obtain Microsoft Support Files from Online Services" Microsoft used the most current virus detection software available on the date of posting to scan this file for viruses. Once posted, the file is housed on secure servers that prevent any unauthorized changes to the file.

  FileName       Size       Description -

Makefile       267 bytes   This file indirects to the real make file that is shared by all of the driver components of the Windows NT DDK.

Sources        127 bytes   Generic file that lists source files and all the build options.

Testapp.c      4.01KB      Source file of the test application. section.htm    6KB         Sample Tour documentation for this sample (this file).

section.c      15KB section.h      2KB

section.ini    196 bytes   .ini file for setting up the registry to                               load the driver.

section.rc     255 bytes   Resource file that specify information such as file type, version, and so forth.

sources        98 bytes

In this sample, a console application (SecTest.exe) creates a named memory- mapped file backed by pagefile in user mode with CreateFileMapping, maps a view of the file (MapViewOfFile), writes a message in the file, and makes an ioctl call to the driver. The driver opens the same memory object in its dispatch routine using ZwOpenSection function, calls ZwMapViewOfSection to get a pointer to it, reads the message, and writes a reply at the same location for the application to read. Since the virtual address received by the driver as a result of mapping falls in the user address space, the driver routine should run in the context of the process to access it. Please read the comments in the Section.c source file to learn more about the driver design.

The building and installation instruction given here apply to Windows NT 3.51 and 4.0.

NOTE: This driver provides an example of a minimal driver. Neither it, nor the sample programs are intended to be used in a production environment. Rather, they are intended for educative purposes and as a skeleton driver.

Building the Sample

 * 1) Click the "Free Build Environment" or "Checked Build Environment" icon under your "Windows NT DDK" program's group to set basic environment variables needed by Windows NT.
 * 2) Change to the directory containing the device source code, such as cd src\general\section.
 * 3) Run "BUILD -cef," or use the macro "BLD." This behavior invokes the Microsoft make routines that produce log files called Build.log, Build.wrn, and Build.err. The .wrn file contains warnings. If the build succeeds, the driver is named Section.sys, and you can find it in the I386 subdirectory of your  directory specified in the Sources file. The SecTest.exe is built and placed in the same output directory.

Installing the Driver
 Copy the Section.sys file from the \Lib\I386\ subdirectory created by BUILD to the \SYSTEM32\DRIVERS directory of the target computer where all the Windows NT device drivers are stored. If the system responds "ACCESS DENIED," a previous version of the file is currently in use and Windows NT will not let you replace it. You can unload it by typing "net stop section". Copy SecTest.exe from the \Lib\I386\ subdirectory created by BUILD to your target \ where you will exercise the test. Install the section driver as follows:

 Copy <BASEDIR>\src\general\section\sys\section.ini to anywhere in your target computer so you can initialize the registry for this driver.</li> Use the REGINI utility to create the entries listed in the Section.ini file. For example:

Regini section.ini

NOTE: You must do this with "Administrator" privileges.

NOTE: This adds a section driver key to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\section tree in the registry. You can verify this by running RegEdt32.exe and looking in this location.</li> Restart the computer under test.</li></ol> </li></ol>

Testing the Driver
To test section driver, run the SecTest.exe (a simple Win32 single-threaded console mode application).