home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_04 / DDISpy / DDISpy.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  4.8 KB  |  164 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   : ddispy.cpp                                                             //
  10. //  Description: DDI spy device driver portion                                       //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #include "kernelopt.h"
  15. #include "ddispy.h"
  16.  
  17.  
  18. const WCHAR DeviceName[] = L"\\Device\\DDISpy";
  19. const WCHAR DeviceLink[] = L"\\DosDevices\\DDISPY";
  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 ( (ioBuffer!=NULL) && 
  50.         (irpStack->Parameters.DeviceIoControl.InputBufferLength >= 8) )
  51.     {
  52.         unsigned leng = ioBuffer[1];
  53.  
  54.         if ( irpStack->Parameters.DeviceIoControl.OutputBufferLength >= leng )
  55.         {
  56.             switch ( irpStack->Parameters.DeviceIoControl.IoControlCode )
  57.             {
  58.                 case DDISPY_REPORT:
  59.                     ioBuffer[0] = (unsigned) & Buffer[0];    // read for DDI login buffer
  60.  
  61.                 case DDISPY_READ:
  62.                     {
  63.                         Irp->IoStatus.Information = leng;
  64.                         nStatus = STATUS_SUCCESS;
  65.  
  66.                         __try
  67.                         {
  68.                             memcpy(ioBuffer, (void *) ioBuffer[0], leng);
  69.                         }
  70.                         __except ( EXCEPTION_EXECUTE_HANDLER )
  71.                         {
  72.                             Irp->IoStatus.Information = 0;
  73.                             nStatus = STATUS_INVALID_PARAMETER;
  74.                         }
  75.                     }
  76.                     break;
  77.  
  78.                 case DDISPY_START:
  79.                     {
  80.                         Irp->IoStatus.Information = leng;
  81.                         
  82.                         DDISpy_Start(ioBuffer[0], ioBuffer[1]);
  83.  
  84.                         memcpy(ioBuffer, "YuanFeng", 8);
  85.                         
  86.                         nStatus = STATUS_SUCCESS;
  87.                     }
  88.                     break;
  89.  
  90.                 case DDISPY_END:
  91.                     {
  92.                         Irp->IoStatus.Information = leng;
  93.  
  94.                         DDISpy_Stop(ioBuffer[0], ioBuffer[1]);
  95.  
  96.                         memcpy(ioBuffer, "FengYuan", 8);
  97.  
  98.                         nStatus = STATUS_SUCCESS;
  99.                     }
  100.                     break;
  101.             }
  102.         }
  103.     }
  104.     
  105.     Irp->IoStatus.Status = nStatus;
  106.     IoCompleteRequest(Irp, IO_NO_INCREMENT);
  107.    
  108.     return nStatus;
  109. }
  110.  
  111.  
  112. // Process driver unloading
  113. void DrvUnload(IN PDRIVER_OBJECT DriverObject)
  114. {
  115.     UNICODE_STRING deviceLinkUnicodeString;
  116.  
  117.     RtlInitUnicodeString(&deviceLinkUnicodeString, DeviceLink);
  118.  
  119.     IoDeleteSymbolicLink(&deviceLinkUnicodeString);
  120.     IoDeleteDevice(DriverObject->DeviceObject);
  121. }
  122.  
  123.  
  124. // Installable driver initialization entry point.
  125. NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, IN PUNICODE_STRING RegistryPath)
  126. {
  127.     UNICODE_STRING  deviceNameUnicodeString;
  128.     
  129.     RtlInitUnicodeString( &deviceNameUnicodeString, DeviceName );
  130.  
  131.     // Create a device
  132.     PDEVICE_OBJECT  deviceObject = NULL;
  133.     
  134.     NTSTATUS ntStatus = IoCreateDevice (Driver,
  135.         sizeof(KDeviceExtension), & deviceNameUnicodeString,
  136.         FILE_DEVICE_DDISPY, 0, TRUE, & deviceObject);
  137.  
  138.     if ( NT_SUCCESS(ntStatus) )
  139.     {
  140.         // Create a symbolic link that Win32 apps can specify
  141.         // to gain access to this driver/device
  142.         UNICODE_STRING  deviceLinkUnicodeString;
  143.  
  144.         RtlInitUnicodeString (&deviceLinkUnicodeString, DeviceLink);
  145.  
  146.         ntStatus = IoCreateSymbolicLink(& deviceLinkUnicodeString,
  147.                                         & deviceNameUnicodeString);
  148.  
  149.         // Create driver dispatch table
  150.         if ( NT_SUCCESS(ntStatus) )
  151.         {
  152.             Driver->DriverUnload                         = DrvUnload;
  153.             Driver->MajorFunction[IRP_MJ_CREATE]         = DrvCreateClose;
  154.             Driver->MajorFunction[IRP_MJ_CLOSE]             = DrvCreateClose;
  155.             Driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDeviceControl;
  156.         }
  157.     }
  158.  
  159.     if ( !NT_SUCCESS(ntStatus) && deviceObject!=NULL )
  160.         IoDeleteDevice(deviceObject);
  161.  
  162.     return ntStatus;
  163. }
  164.