#include <ntddk.h>
#include <ntstrsafe.h>
#define IN
#define OUT
NTSTATUS CreateDevice( PDRIVER_OBJECT driver);
void DriverUnload(PDRIVER_OBJECT driver)
{
IoDeleteDevice(driver->DeviceObject);
DbgPrint("DriverUnload...\n");
}
NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
//对一般IRP的简单操作
NTSTATUS status = STATUS_SUCCESS;
DbgPrint("Enter HelloDDKDispatchRoutine\n");
//设置IRP完成状态
pIrp->IoStatus.Status = status;
//设置IRP操作了多少字节
pIrp->IoStatus.Information = 0;
//处理IRP
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
DbgPrint(("Leave HelloDDKDispatchRputine"));
return status;
}
NTSTATUS CreateDevice( PDRIVER_OBJECT pDriverObject)
{
UNICODE_STRING userDevName ;
WCHAR wName[32];
NTSTATUS status;
PDEVICE_OBJECT pDeviceObject;
memset(wName , 0 , sizeof(WCHAR)*32);
RtlStringCchPrintfW(wName , 32 , L"\\Device\\JXTest");
RtlInitUnicodeString(&userDevName , wName);
status = IoCreateDevice(
pDriverObject ,
0 ,
&userDevName ,
FILE_DEVICE_UNKNOWN ,
0 ,
FALSE ,
&pDeviceObject
);
if( STATUS_SUCCESS != status)
{
DbgPrint(" IoCreateDevice Failed,Error Code is %d", status );
}
return status;
}
extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject,PUNICODE_STRING pRegisterPath)
{
NTSTATUS status;
DbgPrint("DriverEntry...\n");
//设置派遣函数
for (int i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
{
pDriverObject->MajorFunction[i] = HelloDDKDispatchRoutine;
}
//设置卸载函数
pDriverObject->DriverUnload = DriverUnload;
//创建设备对象
status = CreateDevice(pDriverObject);
return status;
}
1.建一个目录HelloWorld
2.把HelloWorld.cpp和makefile,SOURCES文件拷贝进去
3.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 components of Windows
#
!INCLUDE $(NTMAKEENV)\makefile.def
4.SOURCES内容
TARGETNAME = HelloWorld
TARGETTYPE = DRIVER
SOURCES = HelloWorld.c
5.进入DDK编译环境,我选择的为win xp check.切换到HelloWorld目录。
6.执行build /w /g #/w会显示所有警告 /g以彩色显示警告/错误/概述
7.如果编译成功,则提示
BUILD: Done
3 files compiled - 0 LPS
1 executable built
8.拷贝到xp虚拟机上
9.运行DbgView查看内核状态下输出,运行Winobj查看创建的设备对象,运行InstDrv进行驱动的加载,启动,停止,卸载。
10.附件为用到的工具。