home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_03 / Periscope / periscope.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  4.2 KB  |  133 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : periscope.cpp                                                         //
  10. //  Description: Periscope kernel mode driver implementation                         //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #include "kernelopt.h"
  15. #include "periscope.h"
  16.  
  17. const WCHAR DeviceName[] = L"\\Device\\Periscope";
  18. const WCHAR DeviceLink[] = L"\\DosDevices\\PERISCOPE";
  19.  
  20.  
  21. // Process CreateFile, CloseHandle
  22. NTSTATUS DrvCreateClose(IN PDEVICE_OBJECT DeviceObject,   
  23.                         IN PIRP           Irp)
  24. {
  25.     Irp->IoStatus.Information = 0;
  26.     Irp->IoStatus.Status      = STATUS_SUCCESS;
  27.     
  28.     IoCompleteRequest(Irp, IO_NO_INCREMENT);
  29.  
  30.     return STATUS_SUCCESS;
  31. }
  32.  
  33.  
  34. // Process DeviceIoControl
  35. NTSTATUS DrvDeviceControl(IN PDEVICE_OBJECT DeviceObject,   
  36.                           IN PIRP           Irp)
  37. {
  38.     NTSTATUS nStatus = STATUS_INVALID_PARAMETER;
  39.  
  40.     Irp->IoStatus.Information = 0;
  41.  
  42.     // Get a pointer to the current location in the Irp,
  43.     // where the function codes and parameters are located.
  44.     PIO_STACK_LOCATION irpStack = 
  45.         IoGetCurrentIrpStackLocation (Irp);
  46.    
  47.     unsigned * ioBuffer = (unsigned *) Irp->AssociatedIrp.SystemBuffer;
  48.             
  49.     if ( (irpStack->Parameters.DeviceIoControl.IoControlCode
  50.          == IOCTL_PERISCOPE) && (ioBuffer!=NULL) && 
  51.         (irpStack->Parameters.DeviceIoControl.
  52.           InputBufferLength >= 8) )
  53.     {
  54.         unsigned leng = ioBuffer[1];
  55.  
  56.         if ( irpStack->Parameters.DeviceIoControl.OutputBufferLength >= leng )
  57.         {
  58.             Irp->IoStatus.Information = leng;
  59.             nStatus = STATUS_SUCCESS;
  60.  
  61.             __try
  62.             {
  63.                 memcpy(ioBuffer, (void *) ioBuffer[0], leng);
  64.             }
  65.             __except ( EXCEPTION_EXECUTE_HANDLER )
  66.             {
  67.                 Irp->IoStatus.Information = 0;
  68.                 nStatus = STATUS_INVALID_PARAMETER;
  69.             }
  70.         }
  71.     }
  72.     
  73.     Irp->IoStatus.Status = nStatus;
  74.     IoCompleteRequest(Irp, IO_NO_INCREMENT);
  75.    
  76.     return nStatus;
  77. }
  78.  
  79.  
  80. // Process driver unloading
  81. void DrvUnload(IN PDRIVER_OBJECT DriverObject)
  82. {
  83.     UNICODE_STRING deviceLinkUnicodeString;
  84.  
  85.     RtlInitUnicodeString(&deviceLinkUnicodeString, DeviceLink);
  86.  
  87.     IoDeleteSymbolicLink(&deviceLinkUnicodeString);
  88.     IoDeleteDevice(DriverObject->DeviceObject);
  89. }
  90.  
  91.  
  92. // Installable driver initialization entry point.
  93. NTSTATUS DriverEntry(IN PDRIVER_OBJECT  Driver, IN PUNICODE_STRING RegistryPath)
  94. {
  95.     UNICODE_STRING  deviceNameUnicodeString;
  96.     
  97.     RtlInitUnicodeString( &deviceNameUnicodeString, DeviceName );
  98.  
  99.     // Create a device
  100.     PDEVICE_OBJECT  deviceObject = NULL;
  101.     
  102.     NTSTATUS ntStatus = IoCreateDevice (Driver,
  103.         sizeof(KDeviceExtension), & deviceNameUnicodeString,
  104.         FILE_DEVICE_PERISCOPE, 0, TRUE, & deviceObject);
  105.  
  106.     if ( NT_SUCCESS(ntStatus) )
  107.     {
  108.         // Create a symbolic link that Win32 apps can specify
  109.         // to gain access to this driver/device
  110.         UNICODE_STRING  deviceLinkUnicodeString;
  111.  
  112.         RtlInitUnicodeString (&deviceLinkUnicodeString, DeviceLink);
  113.  
  114.         ntStatus = IoCreateSymbolicLink(
  115.             &deviceLinkUnicodeString,
  116.             &deviceNameUnicodeString);
  117.  
  118.         // Create driver dispatch table
  119.         if ( NT_SUCCESS(ntStatus) )
  120.         {
  121.             Driver->DriverUnload                 = DrvUnload;
  122.             Driver->MajorFunction[IRP_MJ_CREATE] = DrvCreateClose;
  123.             Driver->MajorFunction[IRP_MJ_CLOSE]  = DrvCreateClose;
  124.             Driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDeviceControl;
  125.         }
  126.     }
  127.  
  128.     if ( !NT_SUCCESS(ntStatus) && deviceObject!=NULL )
  129.         IoDeleteDevice(deviceObject);
  130.  
  131.     return ntStatus;
  132. }
  133.