当前位置:网站首页>Drive development - the first helloddk
Drive development - the first helloddk
2022-07-06 04:50:00 【ma_ de_ hao_ mei_ le】
Friend chain
Non plug and play driver
helloddk.h
// Make sure that the header file is compiled only once
// Because in real projects , A header file may be contained by another header file
// such as b.h It contains a.h
// And then in c.c Has the following code :
/*
#include<a.h>
#include<b.h>
*/
// thus ,a.h It was included twice
// and #pragma once Can guarantee a.h It is compiled only once
// So as to improve the compilation efficiency
#pragma once
// The following condition is compiled in C++ It is very common in projects
// It allows us to C++ Project use C Header file in
#ifdef __cplusplus
extern "C"
{
#endif
#include <NTDDK.h>
#ifdef __cplusplus
}
#endif
// Paging mark 、 Non paged marking and initialization of memory blocks
#define PAGEDCODE code_seg("PAGE")
#define LOCKEDCODE code_seg()
// INIT The flag indicates that the function only needs to be loaded into memory when loading
// After the driver is successfully loaded , Functions can be unloaded from memory
#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]))
// Define body _DEVICE_EXTENSION , And name it
// Device extension structure
// This structure is widely used in drivers
// According to the needs of different procedures , It is used to supplement and define the relevant information of the equipment
typedef struct _DEVICE_EXTENSION {
PDEVICE_OBJECT pDevice;
UNICODE_STRING ustrDeviceName; // Equipment name
UNICODE_STRING ustrSymLinkName; // Symbolic link name
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
// Function declaration
// This IN The keyword may be used to indicate that the parameter is an incoming parameter
NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject);
VOID HelloDDKUnload(IN PDRIVER_OBJECT pDriverObject);
NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
helloddk.cpp
#include "HelloDDK.h"
/*
Initialize the driver , Locate and apply for hardware resources , Create kernel objects
parameter list
pDriverObject: from IO The driver object passed in the manager
pRegistryPath: The path of the driver in the registry
Return value :
Return to initialization driver state
*/
// Use extern "C" Decorate the function , In this way, it will be compiled into [email protected]
// Without this modifier , The compiler will follow C++ The symbolic name of , Link time will report an error
// Indicates that the function is loaded into INIT In the memory area
#pragma INITCODE
extern "C" NTSTATUS DriverEntry(
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING pRegistryPath)
{
NTSTATUS status;
// There are no programs running in the kernel console Of , So you can only use KdPrint Macro to output debugging information
// This macro only works in debug versions (Free)
// Do nothing in the release (Checked)
KdPrint(("Enter DriverEntry\n"));
// Register other driver call function entries
// Send the address of our own defined function to the operating system
// The operating system will call these functions when appropriate
// Through the following assignment operation
// When the driver is unloaded ,HelloDDKUnload Function will be called
// Create in the driver 、 Turn off read-write related IRP when ,HelloDDKDispatchRoutine Function will be called
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;
// Create drive device object
status = CreateDevice(pDriverObject);
KdPrint(("DriverEntry end\n"));
// return CreateDevice Result
return status;
}
// Definition CreateDevice function
#pragma INITCODE
NTSTATUS CreateDevice(
IN PDRIVER_OBJECT pDriverObject)
{
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
// Create device name
// structure Unicode The string is used to store the name of this device object
UNICODE_STRING devName;
RtlInitUnicodeString(&devName, L"\\Device\\MyDDKDevice");
// Create device
status = IoCreateDevice(pDriverObject,
sizeof(DEVICE_EXTENSION),
&(UNICODE_STRING)devName,
FILE_DEVICE_UNKNOWN,
0,
TRUE,
&pDevObj);
if(!NT_SUCCESS(status))
return status;
// Indicates that the device is BUFFERED_IO equipment
// There are two kinds of memory operations of devices , One is BUFFERED_IO, One is DO_DIRECT_IO, I'll explain later
pDevObj->Flags = pDevObj->Flags | DO_BUFFERED_IO;
// Fill in the extended structure of the equipment
pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
pDevExt->pDevice = pDevObj;
pDevExt->ustrDeviceName = devName;
// Create symbolic links
// The device name is only visible in kernel mode , User programs are invisible
// Therefore, a symbolic connection needs to be exposed , The symbolic link points to the real device name
UNICODE_STRING SymLinkName;
RtlInitUnicodeString(&SymLinkName, L"\\??\\HelloDDK");
pDevExt->ustrSymLinkName = SymLinkName;
// If it is created successfully, it will return , Otherwise, call IoDeleteDevice Delete device
status = IoCreateSymbolicLink(&SymLinkName, &devName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
return status;
}
return STATUS_SUCCESS;
}
// Define the driver unload function
#pragma PAGEDCODE
VOID HelloDDKUnload(IN PDRIVER_OBJECT pDriverObject)
{
PDEVICE_OBJECT pNextObj;
KdPrint(("Enter DriverUnload\n"));
// Get the device object from the driver object
pNextObj = pDriverObject->DeviceObject;
// Traverse the device object
while(pNextObj != NULL)
{
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension;
// Remove symbolic links
UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName;
// Delete symbolic links of device objects
IoDeleteSymbolicLink(&pLinkName);
pNextObj = pNextObj->NextDevice;
IoDeleteDevice(pDevExt->pDevice);
}
}
// Define default dispatch routines
#pragma PAGEDCODE
NTSTATUS HelloDDKDispatchRoutine(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp)
{
KdPrint(("Enter HelloDDKDispatchRoutine\n"));
NTSTATUS status = STATUS_SUCCESS;
// complete IRP
// About IRP Introduction to , I'll introduce it later
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
KdPrint(("Leave HelloDDKDispatchRoutine\n"));
return status;
}
source
TARGETNAME=HelloDDK
TARGETTYPE=DRIVER
TARGETPATH=OBJ
INCLUDES=$(BASEDIR)\inc;\
$(BASEDIR)\inc\ddk;\
SOURCES=helloddk.cpp\
边栏推荐
- I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
- RTP GB28181 文件测试工具
- 关于es8316的音频爆破音的解决
- 二叉树基本知识和例题
- [FreeRTOS interrupt experiment]
- Dry goods collection | Vulkan game engine video tutorial
- How to estimate the population with samples? (mean, variance, standard deviation)
- 项目经理,你会画原型嘛?项目经理需要做产品设计了?
- Class inheritance in yyds dry inventory C
- Codeforces Round #804 (Div. 2)
猜你喜欢

The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder

8. Static file

Visio draws Tai Chi

Postman管理测试用例
![[数学建模] 微分方程--捕鱼业的持续发展](/img/7c/2ab6f2a34bc2c97318537ec8e0b0c5.png)
[数学建模] 微分方程--捕鱼业的持续发展

Codeforces Round #804 (Div. 2)

Microblogging hot search stock selection strategy

Uva1592 Database

Lepton 无损压缩原理及性能分析

Weng Kai C language third week 3.1 punch in
随机推荐
Leetcode 186 Flip the word II in the string (2022.07.05)
Introduction of several RS485 isolated communication schemes
项目经理,你会画原型嘛?项目经理需要做产品设计了?
Codeforces Round #804 (Div. 2)
Excellent PM must experience these three levels of transformation!
What are the advantages of the industry private network over the public network? What specific requirements can be met?
RT thread analysis - object container implementation and function
Digital children < daily question> (Digital DP)
Basic explanation of turtle module - draw curve
ue5 小知识 FreezeRendering 查看视锥内渲染的物体
[数学建模] 微分方程--捕鱼业的持续发展
[noip2009 popularization group] score line delimitation
canal同步mysql数据变化到kafka(centos部署)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
Crazy God said redis notes
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
关于imx8mp的es8316的芯片调试
Word cover underline
[try to hack] John hash cracking tool