本人是个驱动程序的初学者,写了一个简单的NT驱动例子,在其中使用KdPrint输出一些信息,但是信息不能正常显示.
我使用的是XP,
使用注册表的方式在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services子键下创建一个新的子项HelloDDK,再创建上相应的键值:
DisplayName(REG_SZ) :HelloDDK
ErrorControl(REG_DWORD) :0x00000000
ImagePath(REG_EXPAND_SZ):\??\e:\MyDriver\HelloDDK.sys
Start(REG_DWORD) :0x00000003
Type(REG_DWORD) :0x00000001
在CMD中执行:
net start HelloDDK命令 显示服务正常启动.
net stop HelloDDK命令 显示服务正常停止.
还有我使用的是最新的WDK,可以在Win2008上使用的.
是使用VS2005编译的,编译路径的设置如下:
INCLUDE:
C:\WinDDK\6001.18001\inc\ddk
C:\WinDDK\6001.18001\inc\api
(提升到了最上)
LIB:
C:\WinDDK\6001.18001\lib\wxp\i386
(提升到了最上)
以下是我的源代码, 很简单,请各位帮忙:
头文件:
- #pragma once
- #include
- #define PAGEDCODE code_seg("PAGE")
- #define LOCKEDCODE code_seg()
- #define INITCODE code_seg("INIT")
- #define PAGEDDATA data_seg("PAGE")
- #define LOCKEDDATA data_seg()
- #define INITDATA data_seg("INIT")
- #define arraysize(p) (sizeof(p)/sizeof((p)[0]))
- typedef struct _DEVICE_EXTENSION {
- PDEVICE_OBJECT pDevice;
- UNICODE_STRING ustrDeviceName; //设备名称
- UNICODE_STRING ustrSymLinkName; //符号链接名
- } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
- // 函数声明
- NTSTATUS CreateDevice (IN PDRIVER_OBJECT pDriverObject);
- VOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject);
- NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj,
- IN PIRP pIrp);
-
源文件:
- #include "Driver.h"
- /************************************************************************
- * 函数名称:DriverEntry
- * 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象
- * 参数列表:
- pDriverObject:从I/O管理器中传进来的驱动对象
- pRegistryPath:驱动程序在注册表的中的路径
- * 返回 值:返回初始化驱动状态
- *************************************************************************/
- #pragma INITCODE
- extern "C" NTSTATUS DriverEntry (
- IN PDRIVER_OBJECT pDriverObject,
- IN PUNICODE_STRING pRegistryPath )
- {
- NTSTATUS status;
- KdPrint(("Enter DriverEntry\n"));
- //注册其他驱动调用函数入口
- pDriverObject->DriverUnload = HelloDDKUnload;
- pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKDispatchRoutine;
- pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKDispatchRoutine;
- pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKDispatchRoutine;
- pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKDispatchRoutine;
-
- //创建驱动设备对象
- status = CreateDevice(pDriverObject);
- KdPrint(("DriverEntry end\n"));
- return status;
- }
- /************************************************************************
- * 函数名称:CreateDevice
- * 功能描述:初始化设备对象
- * 参数列表:
- pDriverObject:从I/O管理器中传进来的驱动对象
- * 返回 值:返回初始化状态
- *************************************************************************/
- #pragma INITCODE
- NTSTATUS CreateDevice (
- IN PDRIVER_OBJECT pDriverObject)
- {
- NTSTATUS status;
- PDEVICE_OBJECT pDevObj;
- PDEVICE_EXTENSION pDevExt;
-
- //创建设备名称
- UNICODE_STRING devName;
- RtlInitUnicodeString(&devName,L"\\Device\\MyDDKDevice");
-
- //创建设备
- status = IoCreateDevice( pDriverObject,
- sizeof(DEVICE_EXTENSION),
- &(UNICODE_STRING)devName,
- FILE_DEVICE_UNKNOWN,
- 0, TRUE,
- &pDevObj );
- if (!NT_SUCCESS(status))
- return status;
- pDevObj->Flags |= DO_BUFFERED_IO;
- pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
- pDevExt->pDevice = pDevObj;
- pDevExt->ustrDeviceName = devName;
- //创建符号链接
- UNICODE_STRING symLinkName;
- RtlInitUnicodeString(&symLinkName,L"\\??\\HelloDDK");
- pDevExt->ustrSymLinkName = symLinkName;
- status = IoCreateSymbolicLink( &symLinkName,&devName );
- if (!NT_SUCCESS(status))
- {
- IoDeleteDevice( pDevObj );
- return status;
- }
- return STATUS_SUCCESS;
- }
- /************************************************************************
- * 函数名称:HelloDDKUnload
- * 功能描述:负责驱动程序的卸载操作
- * 参数列表:
- pDriverObject:驱动对象
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- VOID HelloDDKUnload (IN PDRIVER_OBJECT pDriverObject)
- {
- PDEVICE_OBJECT pNextObj;
- KdPrint(("Enter DriverUnload\n"));
- pNextObj = pDriverObject->DeviceObject;
- while (pNextObj != NULL)
- {
- PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
- pNextObj->DeviceExtension;
- //删除符号链接
- UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName;
- IoDeleteSymbolicLink(&pLinkName);
- pNextObj = pNextObj->NextDevice;
- IoDeleteDevice( pDevExt->pDevice );
- }
- }
- /************************************************************************
- * 函数名称:HelloDDKDispatchRoutine
- * 功能描述:对读IRP进行处理
- * 参数列表:
- pDevObj:功能设备对象
- pIrp:从IO请求包
- * 返回 值:返回状态
- *************************************************************************/
- #pragma PAGEDCODE
- NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj,
- IN PIRP pIrp)
- {
- //OutputDebugString(L"AAAAAAAAAAAAAAAAAA");
- KdPrint(("Enter HelloDDKDispatchRoutine\n"));
- NTSTATUS status = STATUS_SUCCESS;
- // 完成IRP
- pIrp->IoStatus.Status = status;
- pIrp->IoStatus.Information = 0; // bytes xfered
- IoCompleteRequest( pIrp, IO_NO_INCREMENT );
- KdPrint(("Leave HelloDDKDispatchRoutine\n"));
- return status;
- }
-