#include<stdio.h>
#include<stdlib.h>
#include "ntddk.h"
#define DEVICE_NAME L"\\Device\\MyDevice"
PUCHAR PsGetProcessImageFileName(PEPROCESS Process);
BOOLEAN ThreadRunFlags = TRUE;
#define WRITE_FILE_INTERVAL -10000 * 1000 * 10
typedef struct my_info {
int age;
int weight;
char* name;
PIO_WORKITEM item;
}myInfo, *PmyInfo;
PDEVICE_OBJECT DeviceObject;
HANDLE hThread;
NTSTATUS
GetLocalTime(OUT PTIME_FIELDS timeFields)
{
NTSTATUS status = STATUS_SUCCESS;
LARGE_INTEGER sysTime, locTime;
KeQuerySystemTime(&sysTime);
ExSystemTimeToLocalTime(&sysTime, &locTime);
RtlTimeToTimeFields(&locTime, timeFields);
return STATUS_SUCCESS;
}
VOID TestFile(IN PDEVICE_OBJECT DeviceObject,
IN PmyInfo pmyInfo)
{
TIME_FIELDS time;
UNICODE_STRING string;
HANDLE hFile;
IO_STATUS_BLOCK iostatus;
NTSTATUS status;
WCHAR pBuffer[200];
OBJECT_ATTRIBUTES objattr;
LARGE_INTEGER ByteOffset;
KIRQL irql;
RtlInitUnicodeString(&string, L"\\??\\C:\\1.log");
InitializeObjectAttributes(&objattr, &string, OBJ_CASE_INSENSITIVE, NULL, NULL);
GetLocalTime(&time);
irql = KeGetCurrentIrql();
KdPrint(("工作线程中的irql=%d", irql));
status = ZwCreateFile(&hFile, FILE_APPEND_DATA,
&objattr, &iostatus,
NULL, FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_WRITE,
FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
swprintf(pBuffer, L"[%d-%d-%d-%d-%d-%d]",
time.Year,
time.Month,
time.Day,
time.Hour,
time.Minute,
time.Second);
KdPrint(("%S age is %d ,weight is %d,name is %s\n",
pBuffer,pmyInfo->age, pmyInfo->weight, pmyInfo->name));
status = ZwWriteFile(hFile, NULL, NULL, NULL, &iostatus,
pBuffer, wcslen(pBuffer) * sizeof(WCHAR), NULL, NULL);
status = ZwWriteFile(hFile, NULL, NULL, NULL, &iostatus,
L"\n", sizeof(WCHAR), NULL, NULL);
ZwClose(hFile);
IoFreeWorkItem(pmyInfo->item);
}
VOID WaitMicroSecond(ULONG ulMircoSecond)
{
KEVENT kEvent;
KeInitializeEvent(&kEvent, SynchronizationEvent, FALSE);
LARGE_INTEGER timeout = RtlConvertLongToLargeInteger(-10 * ulMircoSecond);
KeWaitForSingleObject(&kEvent,
Executive,
KernelMode,
FALSE,
&timeout);
}
VOID ThreadStart(IN PVOID StartContext)
{
PmyInfo pmyInfo;
DbgPrint("Process: %s IRQL:%d\n",
PsGetProcessImageFileName(PsGetCurrentProcess()), KeGetCurrentIrql());
pmyInfo = ExAllocatePool(NonPagedPool, sizeof(myInfo));
pmyInfo->age = 30;
pmyInfo->weight = 80;
pmyInfo->name = "yxp";
while (1 == 1)
{
if (ThreadRunFlags==FALSE)
{
PsTerminateSystemThread(0);
}
PIO_WORKITEM pIoWorkItem = IoAllocateWorkItem(DeviceObject);
if (pIoWorkItem)
{
pmyInfo->item = pIoWorkItem;
IoQueueWorkItem(pIoWorkItem, (PIO_WORKITEM_ROUTINE)TestFile, NormalWorkQueue, pmyInfo);
}
WaitMicroSecond(1000 * 1000);
}
return;
}
VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
ThreadRunFlags = FALSE;
WaitMicroSecond(1000*1000*5);
IoDeleteDevice(DriverObject->DeviceObject);
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
OBJECT_ATTRIBUTES ObjectAttributes;
CLIENT_ID CID;
NTSTATUS status;
UNICODE_STRING DeviceName, Win32Device;
RtlInitUnicodeString(&DeviceName, DEVICE_NAME);
status = IoCreateDevice(DriverObject, 0, &DeviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &DeviceObject);
if (status != STATUS_SUCCESS)
{
DbgPrint("创建设备失败! status=%x\r\n", status);
return status;
}
DriverObject->DriverUnload = DriverUnload;
InitializeObjectAttributes(&ObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
status = PsCreateSystemThread(
&hThread,
GENERIC_READ | GENERIC_WRITE,
&ObjectAttributes,
NtCurrentProcess(),
&CID,
(PKSTART_ROUTINE)ThreadStart,
NULL
);
if (!NT_SUCCESS(status))
{
return 0;
}
ZwClose(hThread);
return STATUS_SUCCESS;
}