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

ID: 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. Click the file name below to download the file:

Section.exe
Release Date: Oct-29-1998

For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.


   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 <TARGETPATH> directory specified in the Sources file. The SecTest.exe is built and placed in the same <i386> output directory.


Installing the Driver

  1. Copy the Section.sys file from the <TARGETPATH>\Lib\I386\<Free or checked build> subdirectory created by BUILD to the <NTROOT>\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".


  2. Copy SecTest.exe from the <TARGETPATH>\Lib\I386\<Free or checked build> subdirectory created by BUILD to your target \<Test directory> where you will exercise the test.


  3. Install the section driver as follows:

    1. Copy <BASEDIR>\src\general\section\sys\section.ini to anywhere in your target computer so you can initialize the registry for this driver.


    2. 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.


    3. Restart the computer under test.




Testing the Driver

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


REFERENCES

http://support.microsoft.com/support/ddk_hardware/ntddk/

http://www.microsoft.com/hwdev/

http://www.microsoft.com/ddk/

Additional query words:

Keywords : kbfile kbsample kbDDK kbKMode kbNTOS400
Version : winnt:4.0
Platform : winnt
Issue type :


Last Reviewed: December 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.