Êíèãà: Writing Windows WDM Device Drivers

Conclusion

Conclusion

This chapter has shown how to set up a development computer for device driver development. A very basic WDM driver has been written and installed in Windows 98 and Windows 2000.

The following chapters explain how to access this driver from a user program and enhance this driver to implement the correct Plug and Play and Power Management handling.

Listing 4.7 Wdm1.h

///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//wdm1.hCommon header
///////////////////////////////////////////////////////////////////////
//Version history
//27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Include WDM standard header with C linkage
#ifdef __cplusplus
extern "C" {
#endif
#include "wdm.h"
#ifdef __cplusplus
}
#endif
///////////////////////////////////////////////////////////////////////
//DebugPrint and Guid headers
#include "DebugPrint.h"
#include "GUIDs.h"
///////////////////////////////////////////////////////////////////////
//Spin lock to protect access to shared memory buffer
extern KSPIN_LOCK BufferLock;
extern PUCHAR Buffer;
///////////////////////////////////////////////////////////////////////
//Our device extension
typedef struct _WDM1_DEVICE_EXTENSION {
 PDEVICE_OBJECT fdo;
 PDEVICE_OBJECT NextStackDevice;
 UNICODE_STRIN GifSymLinkName;
} WDM1_DEVICE_EXTENSION, *PWDM1_DEVICE_EXTENSION;
///////////////////////////////////////////////////////////////////////
// Forward declarations of global functions
VOID Wdm1Unload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS Wdm1Power(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Pnp(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo);
NTSTATUS Wdm1Create(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Close(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Write(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Read(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1DeviceControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1SystemControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
///////////////////////////////////////////////////////////////////////
// NTSTATUS CompleteIrp(PIRP Irp, NTSTATUS status, ULONG info);
///////////////////////////////////////////////////////////////////////

Listing 4.8 Init.cpp

///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//init.cpp:Driver initialization code
///////////////////////////////////////////////////////////////////////
//DriverEntryInitialisation entry point
//Wdm1UnloadUnload driver routine
///////////////////////////////////////////////////////////////////////
//Version history //27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#include "wdm1.h"
#pragma code_seg("INIT") // start INIT section
///////////////////////////////////////////////////////////////////////
//DriverEntry:
//
//Description:
//This function initializes the driver, and creates
//any objects needed to process I/O requests.
//
//Arguments:
//Pointer to the Driver object
//Registry path string for driver service key
//
//Return Value:
//This function returns STATUS_XXX
extern "C"
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {
 NTSTATUS status = STATUS_SUCCESS;
#if DBG
 DebugPrint Init("Wdm1 checked");
#else
 DebugPrintInit("Wdm1 free");
#endif
 DebugPrint("RegistryPath is %T", RegistryPath);
 // Export other driver entry points…
 DriverObject->DriverExtension->AddDevice = Wdm1AddDevice;
 DriverObject->DriverUnload = Wdm1Unload;
 OriverObject->MajorFunction[IRP_MJ_CREATE] = Wdm1Create;
 DriverObject->MajorFunction[IRP_MJ_CLOSE] = Wdm1Close;
 DriverObject->MajorFunction[IRP_MJ_PNP] = Wdm1Pnp;
 DriverObject->MajorFunction[IRP_MJ_POWER] = Wdm1Power;
 DriverObject->MajorFunction[IRP_MJ_READ] = Wdm1Read;
 DriverObject->MajorFunction[IRP_MJ_WRITE] = Wdm1Write;
 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Wdm1DeviceControl;
 DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROl] = Wdm1SystemControl;
 //Initialise spin lock which protects access to shared memory buffer
 KeInitializeSpinLock(&BufferLock);
 DebugPrintMsg("DriverEntry completed");
 return status;
}
#pragma code_seg() // end INIT section
///////////////////////////////////////////////////////////////////////
//Wdm1Unload
//
//Description:
//Unload the driver by removing any remaining objects, etc.
//
//Arguments:
//Pointer to the Driver object
//
//Return Value:
//None
#pragma code_seg("PAGE") // start PAGE section
VOID Wdm1Unload(IN PDRIVER_OBJECT DriverObject) {
 // Free buffer (do not need to acquire spin lock)
 if (Buffer!=NULL) ExFreePool(Buffer);
 DebugPrintMsg("WdmlUnload");
 DebugPrintClose();
}
///////////////////////////////////////////////////////////////////////
#pragma code_seg() // end PAGE section

Listing 4.9 Pnp.cpp

///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//pnp.cpp:Plug and Play and Power IRP handlers
///////////////////////////////////////////////////////////////////////
//Wdm1AddDeviceAdd device routine
//Wdm1PnpPNP IRP dispatcher
//Wdm1PowerPOWER IRP dispatcher
///////////////////////////////////////////////////////////////////////
//Version history //27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#define INITGUID// initialize WDM1_GUID in this module
#include "wdm1.h"
#pragma code_seg("PAGE")// start PAGE section
///////////////////////////////////////////////////////////////////////
//Wdm1AddDevice:
//
//Description:
//Cope with a new Pnp device being added here.
//Usually just attach to the top of the driver stack.
//Do not talk to device here!
//
//Arguments:
//Pointer to the Driver object
//Pointer to Physical Device Object
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS WdmlAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo) {
 DebugPrint("AddDevice");
 NTSTATUS status;
 PDEVICE_OBJECT fdo;
 // Create our Functional Device Object in fdo
 status = IoCreateDevice(DriverObject, sizeof(WDM1_DEVICE_EXTENSION),
  NULL,// No Name
  FILE_DEVICE_UNKNOWN, 0,
  FALSE,// Not exclusive
  &fdo);
 if (!NT_SUCCESS(status)) return status;
 // Remember fdo in our device extension
 PWDM1_DEVICE_EXTENSION dx = (PWDM1_DEVICE_EXTENSION)fdo->DeviceExtension;
 dx->fdo = fdo;
 DebugPrint("FDO is %x",fdo);
 // Register and enable our device interface
 status = IoRegisterDeviceInterface(pdo, &WDM1_GUID, NULL, &dx->ifSymLinkName);
 if (!NT_SUCCESS(status)) {
  IoDeleteDevice(fdo);
  return status;
 }
 IoSetDeviceInterfaceState(&dx->ifSymLinkName, TRUE);
 DebugPrint("Symbolic Link Name is %T", &dx->ifSymLinkName);
 // Attach to the driver stack below us
 dx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo.pdo);
 // Set fdo flags appropriately
 fdo->Flags &= ~DO_DEVICE_INITIALIZING;
 fdo->Flags |= DO_BUFFERED_IO;
 return STATUS_SUCCESS;
}
///////////////////////////////////////////////////////////////////////
//Wdm1Pnp:
//
//Description:
//Handle IRP_MJ_PNP requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//Various minor codes
//IrpStack->Parameters.QueryDeviceRelations
//IrpStack->Parameters.QueryInterface
//IrpStack->Parameters.DeviceCapabilities
//IrpStack->Parameters.FilterResourceRequirements
//IrpStack->Parameters.ReadWriteConfig
//IrpStack->Parameters.SetLock
//IrpStack->Parameters.QueryId
//IrpStack->Parameters.QueryDeviceText
//IrpStack->Parameters.UsageNotification
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Pnp(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 DebugPrint("PnP %I", Irp);
 PWDM1_DEVICE_EXTENSION dx=(PWDMl_DEVICE_EXTENSION)fdo->DeviceExtension;
 // Remember minor function
 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
 ULONG MinorFunction = IrpStack->MinorFunction;
 // Just pass to lower driver
 IoSkipCurrentIrpStackLocation(Irp);
 NTSTATUS status = IoCallDriver(dx->NextStackDevice, Irp);
 // Device removed
 if (MinorFunction==IRP_MN_REMOVE_DEVICE) {
  DebugPrint("PnP RemoveDevice");
  // disable device interface
  IoSetDeviceInterfaceState(&dx->ifSymLinkName, FALSE);
  RtlFreeUnicodeString(&dx->ifSymLinkName);
  // unattach from stack
  if (dx->NextStackDevice) IoDetachDevice(dx->NextStackDevice);
  // delete our fdo IoDeleteDevice(fdo);
 }
 return status;
}
///////////////////////////////////////////////////////////////////////
//Wdm1Power:
//
//Description:
//Handle IRP_MJ_POWER requests
//
//Arguments:
//Pointer to the FDO
//Pointer to the IRP
//IRP_MN_WAIT_WAKE:IrpStack->Parameters.WaitWake.Xxx
//IRP_MN_POWER_SEOUENCE:IrpStack->Parameters.PowerSequence.Xxx
//IRP_MN_SET_POWER:
//IRP_MN_QUERY_POWER:IrpStack->Parameters.Power.Xxx
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Power(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 DebugPrint("Power %I",Irp);
 PWDM1_DEVICE_EXTENSION dx = (PWDM1_DEVICE_EXTENSION)fdo->DeviceExtension;
 // Just pass to lower driver
 PoStartNextPowerIrp(Irp);
 IoSkipCurrentIrpStackLocation(Irp);
 return PoCallDriver(dx->NextStackDevice, Irp);
}
#pragma code_seg()// end PAGE section

Listing 4.10 Dispatch.cpp

///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//dispatch.cpp:Other IRP handlers
///////////////////////////////////////////////////////////////////////
//Wdm1CreateHandle Create/Open file IRP
//Wdm1CloseHandle Close file IRPs
//Wdm1ReadHandle Read IRPs
//Wdm1WriteHandle Write IRPs
//Wdm1DeviceControlHandle DeviceIoControl IRPs
//Wdm1SystemControlHandle WMI IRPs
///////////////////////////////////////////////////////////////////////
//Version history
//27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#include "wdm1.h"
#include "Ioctl.h"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Buffer and BufferSize and guarding spin lock globals (in unpaged memory)
KSPIN_LOCK BufferLock;
PUCHARBuffer = NULL;
ULONGBufferSize = 0;
///////////////////////////////////////////////////////////////////////
//Wdm1Create:
//
//Description:
//Handle IRP_MJ_CREATE requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//IrpStack->Parameters.Create.xxx has create parameters
//IrpStack->FileObject->FileName has file name of device
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Create(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
 DebugPrint("Create File is %T", &(IrpStack->FileObject->FileName);
 // Complete successfully
 return CompleteIrp(Irp,STATUS_SUCCESS,0);
}
///////////////////////////////////////////////////////////////////////
//Wdm1Close:
//
//Description:
//Handle IRP_MJ_CLOSE requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Close(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 DebugPrintMsg("Close");
 // Complete successfully
 return CompleteIrp(Irp,STATUS_SUCCESS,0);
}
///////////////////////////////////////////////////////////////////////
//Wdm1Read:
//
//Description:
//Handle IRP_MJ_READ requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//IrpStack->Parameters.Read.xxx has read parameters
//User buffer at:AssociatedIrp.SystemBuffer(buffered I/O)
//MdlAddress(direct I/O)
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Read(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
 NTSTATUS status = STATUS_SUCCESS;
 LONG BytesTxd = 0;
 // Get call parameters
 LONGLONG FilePointer = IrpStack->Parameters.Read.ByteOffset.QuadPart;
 ULONG ReadLen = IrpStack->Parameters.Read.Length;
 DebugPrint("Read %d bytes from file pointer %d",(int)ReadLen,(int)FilePointer);
 // Get access to the shared buffer
 KIRQL irql;
 KeAcquireSpinLock(&BufferLock,&irql);
 // Check file pointer
 if (FilePointer<0) status = STATUS_INVALID_PARAMETER;
 if (FilePointer>=(LONGLONG)BufferSize) status = STATUS_END_OF_FILE;
 if (status==STATUS_SUCCESS) {
  // Get transfer count
  if ( ((ULONG)FilePointer)+ReadLen>BufferSize) {
   BytesTxd = BufferSize – (ULONG)FilePointer;
   if (BytesTxd<0) BytesTxd = 0;
  } else BytesTxd = ReadLen;
  // Read from shared buffer
  if (BytesTxd>0 && Buffer!=NULL) RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, Buffer+FilePointer, BytesTxd);
 }
 // Release shared buffer
 KeReleaseSpinlock(&BufferLock,irql);
 DebugPrint("Read: %d bytes returned",(int)BytesTxd);
 // Complete IRP
 return Completelrp(Irp.status.BytesTxd);
}
///////////////////////////////////////////////////////////////////////
//Wdm1Write: //
//Description:
//Handle IRP_MJ_WRITE requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//IrpStack->Parameters.Write.xxx has write parameters
//User buffer at:AssociatedIrp.SystemBuffer(buffered I/O)
//MdlAddress(direct I/O)
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Write(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
 NTSTATUS status = STATUS_SUCCESS;
 LONG BytesTxd = 0;
 // Get call parameters
 LONGLONG FilePointer = IrpStack->Parameters.Write.ByteOffset.QuadPart;
 ULONG WriteLen = IrpStack->Parameters.Write.Length;
 DebugPrint("Write %d bytes from file pointer %d",(int)WriteLen,(int)FilePointer);
 if (FilePointer<0) status = STATUS_INVALID_PARAMETER;
 else {
  // Get access to the shared buffer
  KIRQL irql;
  KeAcquireSpinLock(&BufferLock,&irql);
  BytesTxd = WriteLen;
  // (Re)allocate buffer if necessary
  if ( ((ULONG)FilePointer)+WriteLen>BufferSize) {
   ULONG NewBufferSize = ((ULONG)FilePointer)+WriteLen;
   PVOID NewBuffer = ExAllocatePool(NonPagedPool.NewBufferSize);
   if (NewBuffer==NULL) {
    BytesTxd = BufferSize – (ULONG)FilePointer;
    if (BytesTxd<0) BytesTxd = 0;
   } else {
    RtlZeroMemory(NewBuffer,NewBufferSize);
    if (Buffer==NULL) {
     RtlCopyMemory(NewBuffer,Buffer,BufferSize);
     ExFreePool(Buffer);
    }
    Buffer = (PUCHAR)NewBuffer;
    BufferSize = NewBufferSize;
   }
  }
  // Write to shared memory
  if (BytesTxd>0 && Buffer!=NULL) RtlCopyMemory(Buffer+FilePointer, Irp->AssociatedIrp.SystemBuffer, BytesTxd);
  // Release shared buffer
  KeReleaseSpinLock(&BufferLock,irq1);
 }
 DebugPrint("Write: %d bytes written", (int)BytesTxd);
 // Complete IRP
 return CompleteIrp(Irp,status,BytesTxd);
}
///////////////////////////////////////////////////////////////////////
//WdmlDeviceControl:
//
//Description:
//Handle IRP_MJ_DEVICE_CONTROL requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//Buffered:AssociatedIrp.SystemBuffer (and IrpStack-Parameters.DeviceIoControl.Type3InputBuffer)
//Direct:MdlAddress
//
//IrpStack->Parameters.DeviceIoControl.InputBufferLength
//IrpStack->Parameters.DeviceIoControl.OutputBufferLength
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1DeviceControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
 NTSTATUS status – STATUS_SUCCESS;
 ULONG BytesTxd = 0;
 ULONG ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
 ULONG InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
 ULONG OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
 DebugPrint("DeviceIoControl: Control code %x InputLength %d OutputLength %d", ControlCode, InputLength, OutputLength);
 // Get access to the shared buffer KIRQL irql ;
 KeAcquireSpinLock(&BufferLock,&irql);
 switch (ControlCode) {
 ///////Zero Buffer
 case IOCTL_WDM1_ZERO_BUFFER:
  // Zero the buffer
  if (Buffer!=NULL && BufferSize>0) RtlZeroMemory(Buffer,BufferSize);
  break;
 ///////Remove Buffer
 case IOCTL_WDM1_REMOVE_BUFFER:
  if (Buffer!=NULL) {
   ExFreePool(Buffer);
   Buffer = NULL;
   BufferSize = 0;
  }
  break;
 ///////Get Buffer Size as ULONG
 case IOCTL_WDM1_GET_BUFFER_SIZE:
  if (OutputLength<sizeof(ULONG)) status = STATUS_INVALID_PARAMETER;
  else {
   BytesTxd = sizeof(ULONG);
   RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,&BufferSize,sizeof(ULONG));
  }
  break;
 ///////Get Buffer
 case IOCTL_WDM1_GET_BUFFER:
  if (OutputLength>BufferSize) status = STATUS_INVALID_PARAMETER;
  else {
   BytesTxd = OutputLength;
   RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,Buffer,BytesTxd);
  }
  break;
 ///////Invalid request
 default:
  status = STATUS_INVALID_DEVICE_REQUEST;
 }
 // Release shared buffer
 KeReleaseSpinlock(&BufferLock,irql);
 DebugPrint("DeviceIoControl: %d bytes written", (int)BytesTxd);
 // Complete IRP
 return CompleteIrp(Irp,status.BytesTxd);
}
///////////////////////////////////////////////////////////////////////
//Wdm1SystemControl:
//
//Description:
//Handle IRP_MJ_SYSTEM_C0NTROL requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//Various minor parameters
//IrpStack->Parameters.WMI.xxx has WMI parameters
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS WdmlSystemControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
 DebugPrintMsg("SystemControl");
 // Just pass to lower driver
 IoSkipCurrentIrpStackLocation(Irp);
 PWDM1_DEVICE_EXTENSION dx = (PWDM1_DEVICE_EXTENSION)fdo->DeviceExtension;
 return IoCallDriver(dx->NextStackDevice, Irp);
}
///////////////////////////////////////////////////////////////////////
//Wdm1Cleanup:
//
//Description:
//Handle IRP_MJ_CLEANUP requests
//Cancel queued IRPs which match given FileObject
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//IrpStack->FileObject has handle to file
//
//Return Value:
//This function returns STATUS_XXX
//Not needed for Wdm1
///////////////////////////////////////////////////////////////////////
//CompleteIrp:Sets IoStatus and completes the IRP
NTSTATUS CompleteIrp(PIRP Irp, NTSTATUS status, ULONG info) {
 Irp->IoStatus.Status = status;
 Irp->IoStatus.Information = info;
 IoCompleteRequest(Irp,IO_NO_INCREMENT);
 return status;
}
///////////////////////////////////////////////////////////////////////

Listing 4.11 Wdm1.rc

//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
///////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
///////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
///////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifndef _MAC
///////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,5,0
 PRODUCTVERSION 1,0,0,0
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x2L
 FILESUBTYPE 0x0L
BEGIN
 BLOCK "StringFileInfo"
 BEGIN
  BLOCK "080904b0"
  BEGIN
   VALUE "Comments", "Chris Cant"
   VALUE "CompanyName", "PHD Computer Consultants Ltd"
   VALUE "FileDescription", "Wdm1"
   VALUE "FileVersion", "1, 0, 5, 0"
   VALUE "InternalName", "Wdm1 driver"
   VALUE "LegalCopyright", "Copyright © 1998,1999 PHD Computer Consultants Ltd"
   VALUE "OriginalFilename", "Wdm1.sys"
   VALUE "ProductName", "WDM Book"
   VALUE "ProductVersion", "1, 0, 0, 0"
  END
 END
 BLOCK "VarFileInfo"
 BEGIN
  VALUE "Translation", 0x809, 1200
 END
END
#endif // !_MAC
#endif // English (U.S.) resources
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// English (U.K.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
#pragma codepage(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
///////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
 "resource.h"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
 "#include ""afxres.h""rn" ""
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
 "rn" ""
END
#endif // APSTUDIO_INVOKED
#endif // English (U.K.) resources
///////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
///////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
///////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

Listing 4.12 Ioctl.h

//DeviceIoControl IOCTL codes supported by Wdm1
#define IOCTL_WDM1_ZERO_BUFFER CTL_CODE(
 FILE_DEVICE_UNKNOWN,
 0x801,
 METHOD_BUFFERED,
 FILE_ANY_ACCESS)
#define IOCTL_WDM1_REMOVE_BUFFER CTL_CODE(
 FILE_DEVICE_UNKNOWN,
 0x802,
 METHOD_BUFFERED,
 FILE_ANY_ACCESS)
#define IOCTL_WDM1_GET_BUFFER_SIZE CTL_CODE(
 FILE_DEVICE_UNKNOWN,
 0x803,
 METHOD_BUFFERED,
 FILE_ANY_ACCESS)
#define IOCTL_WDM1_GET_BUFFER CTL_CODE(
 FILE_DEVICE_UNKNOWN,
 0x804,
 METHOD_BUFFERED,
 FILE_ANY_ACCESS)
#define IOCTL_WDM1_UNRECOGNISED CTL_CODE(
 FILE_DEVICE_UNKNOWN,
 0x805,
 METHOD_BUFFERED,
 FILE_ANY_ACCESS)

Listing 4.13 GUIDs.h

///////////////////////////////////////////////////////////////////////
//Wdm1 device interface GUID
// {C0CF0640-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(WDM1_GUID, 0xc0cf0640, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
///////////////////////////////////////////////////////////////////////
//Wdm2 device interface GUID
// {C0CF0641-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(WDM2_GUID, 0xc0cf0641, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xd, 0xf3);
///////////////////////////////////////////////////////////////////////
//Wdm3 device interface GUID
// {C0CF0642-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(WDM3_GUID, 0xc0cf0642, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xd, 0xf3);
///////////////////////////////////////////////////////////////////////
//Wdm3 WMI data block GUID
// {C0CF0643-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(WDM3_WMI_GUID, 0xc0cf0643, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
///////////////////////////////////////////////////////////////////////
//Wdm3 WMI event block GUID
// {C0CF0644-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(WDM3_WMI_EVENT_GUID, 0xc0cf0644, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0, 0xdf, 0xe4, 0xc1, 0xf3);
/*
// {C0CF0645-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(<<name>>,
0xc0cf0645, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
// {C0CF0646-5F6E-11d2-B677-00C0DFE4C1F31
DEFINE_GUID(<<name>>,
0xc0cf0646, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
// {C0CF0647-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE GUID(<<name>>,
0xc0cf0647, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
// {C0CF0648-5F6E-11d2-B677-00C0DFE4C1F3}
DEFINE_GUID(<<name>>,
0xc0cf0648, 0x5f6e, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3);
*/
///////////////////////////////////////////////////////////////////////

Listing 4.14 resource.h

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Wdm1.rc
//
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC              1
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE  40001
#define _APS_NEXT_CONTROL_VALUE  1000
#define _APS_NEXT_SYMED_VALUE    101
#endif
#endif

Listing 4.15 Wdm1free.inf

; Wdm1free.Inf – install information file
; Copyright © 1998,1999 Chris Cant, PHD Computer Consultants Ltd
[Version]
Signature="$Chicago$"
Class=Unknown
Provider=%WDMBook%
DriverVer=04/26/1999,1.0.5.0
[Manufacturer]
%WDMRook% = WDM.Book
[WDM.Book]
%Wdm1%=Wdm1.Install, *wdmBookWdm1
[DestinationDirs]
Wdm1.Files.Driver=10,System32Drivers
Wdm1.Files.Driver.NTx86=10,System32Drivers
[SourceDisksNames]
1="Wdm1 build directory",,,
[SourceDisksFiles]
Wdm1.sys=1,obji386free
[SourceDisksFiles.x86] Wdm1.sys=1,objfrei386
; Windows 98
[Wdm1.Install]
CopyFiles=Wdm1.Files.Driver
AddReg=Wdm1.AddReg
[Wdm1.AddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,Wdm1.sys
[Wdm1.Files.Driver]
Wdm1.sys
; Windows 2000
[Wdm1.Install.NTx86]
CopyFiles=Wdm1.Files.Driver.NTx86
[Wdm1.Files.Driver.NTx86]
Wdm1.sys,,,%COPYFLG_NOSKIP%
[Wdm1.Install.NTx86.Services]
AddService = Wdm1, %SPSVCINST_ASSOCSERVICE%, Wdm1.Service
[Wdm1.Service]
DisplayName = %Wdm1.ServiceName%
ServiceType = %SERVICE_KERNEL_DRIVER%
StartType = %SERVICE_DEMAND_START%
ErrorControl = %SERVICE_ERROR_NORMAL%
ServiceBinary = %10%System32DriversWdm1.sys
; Strings
[Strings]
WDMBook="WDM Book"
Wdm1="WDM Book: Wdm1 Example, free build"
Wdm1.ServiceName="WDM Book Wdm1 Driver"
SPSVCINST_ASSOCSERVICE=0x00000002; Driver service is associated with device being installed
COPYFLG_NOSKIP=2; Do not allow user to skip file
SERVICE_KERNEL_DRIVER=1
SERVICE_AUTO_START=2
SERVICE_DEMAND_START=3
SERVICE_ERROR_NORMAL=1

Listing 4.16 SOURCES

TARGETNAME=Wdm1
TARGETTYPE=DRIVER
DRIVERTYPE=WDM
TARGETPATH=OBJ
INCLUDES=$(BASEDIR)inc;
SOURCES=init.cpp
 dispatch.cpp
 pnp.cpp
 DebugPrint.c
 Wdm1.rc
NTTARGETFILES=PostBuildSteps

Listing 4.17 makefile.inc

PostBuildSteps: $(TARGET)
!if "$(DDKBUILDENV)"="free"
 rebase –B 0x10000 –X . $(TARGET)
!endif
 copy $(TARGET) $(WINDIR)system32drivers

Listing 4.18 makefile

#
# DO NOT EDIT THIS FILE!!! Edit .sources. if you want to add a new. source
# file to this component. This file merely indirects to the real make file
# that is shared by all the driver components of the Windows NT DDK
#
!INCLUDE $(NTMAKEENV)makefile.def

Listing 4.19 MakeDrvr.bat

@echo off
if "%1"=="" goto usage
if "%3"=="" goto usage
if not exist %1binsetenv.bat goto usage
call %1binsetenv %1 %4
%2
cd %3
build –b –w %5 %6 %1 %8 %9
goto exit
:usage
echo usage MakeDrvr DDK_dir Driver_Drive Driver_Dir free/checked [build_options]
echo eg MakeDrvr %%DDKROOT%% C: %%WDMBOOK%% free –cef
:exit

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.050. Çàïðîñîâ Ê ÁÄ/Cache: 0 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç