📄 simpldrv.c
字号:
/*++
Copyright (c) 1993 Microsoft Corporation
Module Name:
$$safe_root$$.c
Abstract:
A simple kernel-mode driver sample.
Environment:
kernel mode only
Notes:
See readme.txt
Revision History:
06-25-93 : created
--*/
#include "ntddk.h"
#include "stdarg.h"
#include "stdio.h"
//#include "$$safe_root$$.h"
#include "$$safe_root$$.h"
//
// The following is the debug print macro- when we are building checked
// drivers "DBG" will be defined (by the \ddk\setenv.cmd script), and we
// will see debug messages appearing on the KD screen on the host debug
// machine. When we build free drivers "DBG" is not defined, and calls
// to $$safe_root$$KdPrint are removed.
//
#ifdef DBG
#define $$safe_root$$KdPrint(arg) DbgPrint arg
#else
#define $$safe_root$$KdPrint(arg)
#endif
NTSTATUS
$$safe_root$$Dispatch(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
VOID
$$safe_root$$Unload(
IN PDRIVER_OBJECT DriverObject
);
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This entry point is called directly by the I/O system.
Arguments:
DriverObject - pointer to the driver object
RegistryPath - pointer to a unicode string representing the path
to driver-specific key in the registry
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
PDEVICE_OBJECT deviceObject = NULL;
NTSTATUS ntStatus;
WCHAR deviceNameBuffer[] = L"\\Device\\$$safe_root$$";
UNICODE_STRING deviceNameUnicodeString;
PDEVICE_EXTENSION deviceExtension;
WCHAR deviceLinkBuffer[] = L"\\DosDevices\\$$safe_root$$";
UNICODE_STRING deviceLinkUnicodeString;
$$safe_root$$KdPrint (("$$safe_root$$.SYS: entering DriverEntry\n"));
//
// A real driver would:
//
// 1. Report it's resources (IoReportResourceUsage)
//
// 2. Attempt to locate the device(s) it supports
//
// OK, we've claimed our resources & found our h/w, so create
// a device and initialize stuff...
//
RtlInitUnicodeString (&deviceNameUnicodeString,
deviceNameBuffer
);
//
// Create an EXCLUSIVE device, i.e. only 1 thread at a time can send
// i/o requests.
//
ntStatus = IoCreateDevice (DriverObject,
sizeof (DEVICE_EXTENSION),
&deviceNameUnicodeString,
FILE_DEVICE_$$safe_root$$,
0,
TRUE,
&deviceObject
);
if (NT_SUCCESS(ntStatus))
{
deviceExtension = (PDEVICE_EXTENSION) deviceObject->DeviceExtension;
//
// Set up synchronization objects, state info,, etc.
//
//
// Create a symbolic link that Win32 apps can specify to gain access
// to this driver/device
//
RtlInitUnicodeString (&deviceLinkUnicodeString,
deviceLinkBuffer
);
ntStatus = IoCreateSymbolicLink (&deviceLinkUnicodeString,
&deviceNameUnicodeString
);
if (!NT_SUCCESS(ntStatus))
{
$$safe_root$$KdPrint (("$$safe_root$$.SYS: IoCreateSymbolicLink failed\n"));
}
//
// Create dispatch points for device control, create, close.
//
DriverObject->MajorFunction[IRP_MJ_CREATE] =
DriverObject->MajorFunction[IRP_MJ_CLOSE] =
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = $$safe_root$$Dispatch;
DriverObject->DriverUnload = $$safe_root$$Unload;
}
//done_DriverEntry:
if (!NT_SUCCESS(ntStatus))
{
//
// Something went wrong, so clean up (free resources, etc.)
//
if (deviceObject)
IoDeleteDevice (deviceObject);
}
return ntStatus;
}
NTSTATUS
$$safe_root$$Dispatch(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
/*++
Routine Description:
Process the IRPs sent to this device.
Arguments:
DeviceObject - pointer to a device object
Irp - pointer to an I/O Request Packet
Return Value:
--*/
{
PIO_STACK_LOCATION irpStack;
PDEVICE_EXTENSION deviceExtension;
PVOID ioBuffer;
ULONG inputBufferLength;
ULONG outputBufferLength;
ULONG ioControlCode;
NTSTATUS ntStatus;
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
//
// Get a pointer to the current location in the Irp. This is where
// the function codes and parameters are located.
//
irpStack = IoGetCurrentIrpStackLocation (Irp);
//
// Get a pointer to the device extension
//
deviceExtension = DeviceObject->DeviceExtension;
//
// Get the pointer to the input/output buffer and it's length
//
ioBuffer = Irp->AssociatedIrp.SystemBuffer;
inputBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
switch (irpStack->MajorFunction)
{
case IRP_MJ_CREATE:
$$safe_root$$KdPrint (("$$safe_root$$.SYS: IRP_MJ_CREATE\n"));
break;
case IRP_MJ_CLOSE:
$$safe_root$$KdPrint (("$$safe_root$$.SYS: IRP_MJ_CLOSE\n"));
break;
case IRP_MJ_DEVICE_CONTROL:
$$safe_root$$KdPrint (("$$safe_root$$.SYS: IRP_MJ_DEVICE_CONTROL\n"));
ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
switch (ioControlCode)
{
case IOCTL_$$safe_root$$_HELLO:
{
//
// Some app is saying hello
//
break;
}
default:
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
$$safe_root$$KdPrint (("$$safe_root$$.SYS: unknown IRP_MJ_DEVICE_CONTROL\n"));
break;
}
break;
}
//
// DON'T get cute and try to use the status field of
// the irp in the return status. That IRP IS GONE as
// soon as you call IoCompleteRequest.
//
ntStatus = Irp->IoStatus.Status;
IoCompleteRequest (Irp,
IO_NO_INCREMENT
);
//
// We never have pending operation so always return the status code.
//
return ntStatus;
}
VOID
$$safe_root$$Unload(
IN PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the allocated resources, etc.
Arguments:
DriverObject - pointer to a driver object
Return Value:
--*/
{
WCHAR deviceLinkBuffer[] = L"\\DosDevices\\$$safe_root$$";
UNICODE_STRING deviceLinkUnicodeString;
//
// Free any resources
//
//
// Delete the symbolic link
//
RtlInitUnicodeString (&deviceLinkUnicodeString,
deviceLinkBuffer
);
IoDeleteSymbolicLink (&deviceLinkUnicodeString);
//
// Delete the device object
//
IoDeleteDevice (DriverObject->DeviceObject);
$$safe_root$$KdPrint (("$$safe_root$$.SYS: unloading\n"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -