获取时间
我们需要用一个large intger 来存系统时间戳,
KeQuerySystemTime(&system_time); 获取系统时间戳
ExSystemTimeToLocalTime(&system_time,&local_time); 将系统时间戳转为本地区的时间
RtlTimeToTimeFields(&local_time,&local_time_fileds); 将本地时间转为time_fileds,年月日时间类型
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {DriverObject->DriverUnload = Unload;PCHAR string;LARGE_INTEGER system_time = { 0 };LARGE_INTEGER local_time = { 0 };TIME_FIELDS local_time_fileds = { 0 };KeQuerySystemTime(&system_time);ExSystemTimeToLocalTime(&system_time,&local_time);RtlTimeToTimeFields(&local_time,&local_time_fileds);DbgPrint("time is:%4d-%2d-%2d %2d-%2d-%2d,\r\n",local_time_fileds.Year,local_time_fileds.Month,local_time_fileds.Day,local_time_fileds.Hour,local_time_fileds.Minute,local_time_fileds.Second);return STATUS_SUCCESS;}
延迟函数
KeDelayExecutionThread(KernelMode,FALSE,)
- 等待模式
- 是否可变
- 等待时间,可以是负值或绝对值,通常用负值来表示相对时间,单位为100纳秒
```c
include
define DELAY_ONE_MICROSEC (-10) //微秒
define DELAY_ONE_MILLISEC (DELAY_ONE_MICROSEC*1000)//毫秒
VOID Unload(IN PDRIVER_OBJECT DriverObject) { DbgPrint(“driver unload\r\n”); }
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
DriverObject->DriverUnload = Unload;PCHAR string;LARGE_INTEGER system_time = { 0 };LARGE_INTEGER local_time = { 0 };TIME_FIELDS local_time_fileds = { 0 };LARGE_INTEGER interval = { 0 };//5秒int mytime = 5 * 1000;//interval.QuadPart = mytime*DELAY_ONE_MILLISEC ; //100nanoKeDelayExecutionThread(KernelMode, FALSE, &interval);//需要一个annosecond值,我们设置一个毫秒宏DbgPrint("delay loading");return STATUS_SUCCESS;
}
<a name="bKSdz"></a># 设置计时器使用KeSetTimer,我们还需要PDC来设置计时器<br />KeSetTimer 有3个参数:- timer- 过期时间- DPC定时器初始化非常简单,定义一个KTIMER变量,使用KeinitializeTImer 对其初始化即可定时器初始完后是pdc,它有3个参数:- KDPC地址- 你要运行 的函数- 给自定义的函数上下文```c#include <ntddk.h>#define DELAY_ONE_MICROSEC (-10)#define DELAY_ONE_MILLISEC (DELAY_ONE_MICROSEC*1000)KTIMER myTimer;KDPC myDpc;LARGE_INTEGER due;VOID Unload(IN PDRIVER_OBJECT DriverObject) {KeCancelTimer(&myTimer);//取消定时器DbgPrint("driver unload\r\n");}//这个函数没有在PASS_LEVEL 中运行(绝大多数函数在此级别运行)//而是在DISPATCH_LEVEL中运行,这意味着我们不能使用分页内存VOID myDpcFunc(IN PKDPC Dpc,IN PVOID context,IN PVOID SysArgment1,IN PVOID SysArgment2 ) {DbgPrint("timer working..\r\n");KeSetTimer(&myTimer, due, &myDpc);//函数执行完后重置计时器}NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {DriverObject->DriverUnload = Unload;due.QuadPart = 5000 * DELAY_ONE_MILLISEC;//5秒延迟KeInitializeTimer(&myTimer);KeInitializeDpc(&myDpc,myDpcFunc,NULL);//没有参数 context 为nullKeSetTimer(&myTimer,due,&myDpc);return STATUS_SUCCESS;}

