实测在winxp_sp3_x86下顺利运行,并得到符合的结果。
Demo_1为内核字符串的一些演示
Demo_2为对链表和内存的一些操作,以及如何改进成多线程安全
Demo_3为对文件的一些操作,以及copyFile的实现
Demo_4为注册表操作
Demo_5测试时间,这里内核有一个正负数时间的问题,这里很多资料上是没有的,这里我讲一下,内核里时间是分正负的,正负不是代表数值,数值均为绝对值,符号代表着absolute time 和 relative time 当时间为正数时,表示是一个绝对时间,指的是开机到现在的时间,可以用正数定在某个时间后,比如开机半小时等,relative time是一般所说的时间间隔,及延时xx时间,并且内核里一般是以100ns作为一个时间单位的。1 ms = 1ns, 1 s = 1 ms 是这样的关系。
Demo_6测试定时器
Demo_7是内核的基础线程
Demo_8是内核事件的基础
#include <ntddk.h>
#include <ntstrsafe.h>
#pragma warning(disable:4127)
#define MEM_TAG 'Tag1'
#define EACH_TURN_LEN 4 * 1024 //一次写4kb
LIST_ENTRY list_head;//链表头
KSPIN_LOCK spinLock;//自旋锁
typedef struct
{
LIST_ENTRY list_entry;
PFILE_OBJECT file_obj;
UNICODE_STRING file_name;
LARGE_INTEGER len;
}MY_FILE_INFOR, *PMY_FILE_INFOR;
//测试内核字符串
//注意:内核里的字符串不是以零为结尾的 切记!!!所有的常见字符串函数都要慎用
void Demo_1()//字符串演示
{
UNICODE_STRING uniString = RTL_CONSTANT_STRING(L"my first string");//只能初始化的时候调用 constant string
KdPrint(("%wZ\n", &uniString));//这里传进去指针!!!
UNICODE_STRING uniString_2;//这次不进行初始化
RtlInitUnicodeString(&uniString_2, L"my second string");
DbgPrint("%wZ\n", &uniString_2);
//若不是常量字符串的情况下 初始化字符串的第一种方法
UNICODE_STRING uniString_3;
WCHAR buffer[256] = {
0 };
RtlInitEmptyUnicodeString(&uniString_3, buffer, 256 * sizeof(WCHAR));
wcscpy(buffer, L"my third string");
uniString_3.Length = (USHORT)wcslen(buffer) * sizeof(WCHAR);
uniString_3.MaximumLength = 256;
DbgPrint("string : %wZ\n", &uniString_3);
RtlCopyUnicodeString(&uniString_3, &uniString);//拷贝字符串
DbgPrint("string : %wZ\n", &uniString_3);
NTSTATUS status;
status = RtlAppendUnicodeToString(&uniString_3, L"appending!");//附加字符串 连接两个的时候用 RtlAppendUnicodeStringToString ()
if (STATUS_SUCCESS != status) {
DbgPrint("Error in RtlAppendUnicodeToString\n");
}
DbgPrint("string : %wZ\n", &uniString_3);
status = RtlStringCbPrintfW(uniString_3.Buffer, 256 * sizeof(WCHAR), L"string : %wZ", &uniString_2);//类似于sprintf 不过比sprintf安全
if (!NT_SUCCESS(status)) {
DbgPrint("Error!\n");
}
uniString_3.Length =(USHORT) (wcslen(uniString_3.Buffer) * sizeof(WCHAR));
uniString_3.MaximumLength = 256;
DbgPrint("%wZ\n", &uniString_3);
//下面开始动态分配内存
UNICODE_STRING srcString = RTL_CONSTANT_STRING(L"source string!");
UNICODE_STRING uniString_4 = {
0 };
uniString_4.Buffer = (PWCHAR)ExAllocatePoolWithTag(NonPagedPool, srcString.Length, MEM_TAG);
if (!uniString_4.Buffer) {
//这里是错误处理
DbgPrint ("Error in ExAllocatePoolWithTag!\n");
}
//分配完后记得填充length等信息
uniString_4.Length = uniString_4.MaximumLength = srcString.Length;
RtlCopyUnicodeString(&uniString_4, &srcString);//进行字符串拷贝
DbgPrint("The alloc: %wZ\n", &uniString_4);
//最后要进行释放 养成好习惯
ExFreePool(uniString_4.Buffer);
uniString_4.Buffer = NULL;
uniString_4.Length = uniString_4.Maximu