[Compuware Corporation] [Compuware NuMega home page]                [NuMega Lab]
[teal]

 [DriverStudio]    [Image][Image]
   Home
 [Driver Products]        Driver Technical Tips
   DriverStudio          How to Share an Event between Kernel Mode and User Mode
   DriverBundle
   Previews              The recommended way to share memory between user mode and kernel mode is
   Compatibility         to take advantage of the memory locking and address translation
 [Downloads]             mechanisms that the system uses for passing requests to drivers. An
                          application simply passes the address and size of the buffer to be
 Wizards                  shared as a parameter to DeviceIoControl, and the system automatically
   Utilities             locks it into memory and provides a system address for the driver. There
   NT source             are just a few things to remember when setting up this kind of
 examples                 mechanism.
   VxD source
 examples                 First of all, the IOCTL code that the application uses when passing the
   WDM source            buffer must be constructed using the correct buffer method, namely
 examples                 METHOD_OUT_DIRECT. For example,
 [Resources]  
 Technical papers                 #define IOCTL_MYDRIVER_SHARE_BUFFER \
   Useful links
   Technical tips            CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
 [Support]  
 Support                  Second, the call to DeviceIoControl must use asynchronous completion,
   Knowledge base        which is also known as "overlapped I/O". Here is some code that shows
   Problem               how that is done:
 submission
   Product                       HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 registration             OVERLAPPED ov;
   Release notes         memset (&ov, 0, sizeof(OVERLAPPED));
 [Shop NuMega]           ov.hEvent = hEvent;
 Buy it!                  DeviceIoControl(
   Price list                    hDevice,
   How to buy                    IOCTL_MYDRIVER_SHARE_BUFFER,
   Sales offices                 NULL,
                                  0,
                                  BufferToBeShared,
 [Y2K Compliance]                 SizeOfBufferToBeShared,
                                  &nBytesReturned,
                                  &ov
 [More information]               );


                          Remember that you must open the device with FILE_FLAG_OVERLAPPED in the
                          call to CreateFile in order for this to work, and that all calls through
                          that handle must supply an overlap structure.

                          In the driver, the handler for IRP_MJ_DEVICE_CONTROL obtains the system
                          address of the buffer via the MDL in the IRP, and then pends the
                          request. When the buffer is no longer needed, the application calls
                          DeviceIoControl again, this time with code
                          IOCTL_MYDRIVER_DONE_USING_SHARE_BUFFER . The driver then completes the
                          original IRP.

                          NTSTATUS MyDevice::DriverControl(Kirp I)
                          {
                              switch (I.IoctlCode())
                              {
                                  case IOCTL_MYDRIVER_SHARE_BUFFER:
                                      KMemory M(I.Mdl());
                                      m_SharedBuffer = M.MapToSystemSpace();
                                      m_SharedBufferIrp = I;
                                      return STATUS_PENDING;

                                  case IOCTL_MYDRIVER_DONE_USING_SHARE_BUFFER:
                                      m_SharedBufferIrp.Complete(STATUS_SUCCESS);
                                      return I.Complete(STATUS_SUCCESS);
                                  . . .
                                  }
                          . . .
                          }

                          Note: It's a good idea to set a cancel routine on the IRP that holds the
                          buffer, in case something causes the application to terminate
                          abnormally.

                          Back to technical tip start page.

  DriverCentral  DriverStudio  Free downloads  Resources  Support and
                          Services  Shop NuMega
     Compuware NuMega  Tel: +1 603 578-8400  Updated: 9 August 1999 
                      Problems? Contact our webmaster.
