初学驱动开发-windows驱动-helloWorld

1.准备工作-安装vm虚拟机+虚拟机下安装xp

2.下载DriverMonitor+DebugView

3.编写代码

 
#ifdef __cplusplus
extern "C"
{
#endif
#include 
#ifdef __cplusplus
}
#endif 

#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;

/************************************************************************
* 函数名称:LoadDevice
* 功能描述:加载设备对象,如果存在则使用现有设备对象,如果不存在则创建新的设备对象 
*************************************************************************/
#pragma INITCODE
NTSTATUS LoadDevice (
		IN PDRIVER_OBJECT	pDriverObject,
		IN PCWSTR          SourceString)
{
	NTSTATUS status;
	PDEVICE_OBJECT pDevObj;
	PDEVICE_EXTENSION pDevExt; 
	UNICODE_STRING devName;
	//设备名称
	RtlInitUnicodeString(&devName,SourceString);
	
	//创建设备
	status = IoCreateDevice( pDriverObject,
						sizeof(DEVICE_EXTENSION),
						&(UNICODE_STRING)devName,
						FILE_DEVICE_UNKNOWN,
						0, TRUE,
						&pDevObj );
	if (!NT_SUCCESS(status))
    {
		RtlFreeUnicodeString( &devName );
		return status;
	}
	
	RtlZeroMemory(pDevObj->DeviceExtension, sizeof(DEVICE_EXTENSION));
	pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
	pDevObj->Flags |= DO_BUFFERED_IO; 
	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 );
		RtlFreeUnicodeString( &devName );
		RtlFreeUnicodeString( &symLinkName );
		return status;
	} 
	return STATUS_SUCCESS;
}

/************************************************************************
* 函数名称:UnloadDevice 
* 功能描述:卸载驱动设备 
*************************************************************************/
#pragma PAGEDCODE
VOID UnloadDevice (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 );
	}
} 
/************************************************************************
* 函数名称:DriverEntry
* 功能描述:驱动程序入口函数 
*************************************************************************/
#pragma INITCODE
extern "C" NTSTATUS DriverEntry (
			IN PDRIVER_OBJECT pDriverObject,
			IN PUNICODE_STRING pRegistryPath	) 
{
	NTSTATUS status;
	KdPrint(("Enter DriverEntry\n"));
	
	KdPrint(("Hello World Driver\n"));
	KdPrint(("pRegistryPath:%ws\n", pRegistryPath->Buffer)); //信息
	//创建驱动设备对象
	status = LoadDevice(pDriverObject, L"\\Device\\HelloWordDDKDevice");

	//卸载驱动设备对象
	pDriverObject->DriverUnload = UnloadDevice; 
	

	KdPrint(("DriverEntry end\n"));
	return status;
}
编写makefile+sources文件
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

sources 文件
TARGETNAME=HelloDDK
TARGETTYPE=DRIVER
TARGETPATH=OBJ
 
SOURCES=HelloWord.cpp\

初学驱动开发-windows驱动-helloWorld_第1张图片

4.生成.sys文件


5.拷贝.sys文件到虚拟机

6.使用DriverMonitor加载驱动并且用DebugView查看输出





你可能感兴趣的:(C++)