说明
没什么特别的,只是用来测试在WIN10上写驱动,包含最基本的通信代码。
又水了一篇博客,哈哈哈。
注意驱动路径发生改变时,不要忘了修改注册表的值。
https://blog.csdn.net/Kwansy/article/details/113182501
驱动
#include <ntddk.h>
#define DEVICE_NAME L"\\device\\ntdrv"
#define LINK_NAME L"\\dosdevices\\ntdrv"
#define IOCTRL_BASE 0x800
#define MYIOCTRL_CODE(i) \
CTL_CODE(FILE_DEVICE_UNKNOWN, IOCTRL_BASE+i, METHOD_BUFFERED,FILE_ANY_ACCESS)
#define CTL_PRINT MYIOCTRL_CODE(0)
// 通用的分发函数
NTSTATUS DispatchCommon(PDEVICE_OBJECT pObject, PIRP pIrp)
{
pIrp->IoStatus.Status = STATUS_SUCCESS; // 返回给应用层
pIrp->IoStatus.Information = 0; // 读写字节数
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_SUCCESS; // 返回给内核层IO管理器
}
// 我们唯一关心的IRP分发函数
NTSTATUS DispatchIoctrl(PDEVICE_OBJECT pObject, PIRP pIrp)
{
ULONG i;
ULONG nIoctrlCode = 0;
PVOID pInputBuff = NULL;
PVOID pOutputBuff = NULL;
ULONG nInputBufferLength = 0;
ULONG nOutputBufferLength = 0;
ULONG nOutput = 0;
PIO_STACK_LOCATION pStack = NULL;
pInputBuff = pOutputBuff = pIrp->AssociatedIrp.SystemBuffer;
pStack = IoGetCurrentIrpStackLocation(pIrp);
nInputBufferLength = pStack->Parameters.DeviceIoControl.InputBufferLength;
nOutputBufferLength = pStack->Parameters.DeviceIoControl.OutputBufferLength;
nIoctrlCode = pStack->Parameters.DeviceIoControl.IoControlCode;
switch (nIoctrlCode)
{
case CTL_PRINT:
DbgPrint("%s\n", pInputBuff);
for (i = 0; i < strlen(pInputBuff) && i < nInputBufferLength; i++)
{
((PCHAR)pInputBuff)[i] = (UCHAR)toupper(((PCHAR)pInputBuff)[i]);
}
nOutput = nOutputBufferLength;
b