//一般驱动层不使用数据结构,一般Ring3层
双向链表可以将链表形成一个环.BLINK指针指向前一个元素,FLINK指针指向下一个元素.
typedef struct _LIST_ENTRY {
struct _LIST_ENTRY *Flink;
struct _LIST_ENTRY *Blink;
} LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;
初始化
双向链表都是以一个链表头作为链表的第一个元素.初始化链表头就是把Flink和Blink都指向自己,此时就是空链.使用InitializeListHead函数.
1 #include <ntifs.h> 2 3 typedef struct _ITEM_ 4 { 5 union 6 { 7 LIST_ENTRY ListEntry; 8 SINGLE_LIST_ENTRY SingleListEntry; 9 }u; 10 ULONG ItemData; 11 }ITEM,*PITEM; 12 13 14 void SingleListTest();//单链表 15 void ListTest();//双链表 16 17 VOID DriverUnload(PDRIVER_OBJECT DriverObject); 18 19 20 21 22 23 24 25 #include "List.h" 26 27 //bp List!DriverEntry 28 NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegisterPath) 29 { 30 NTSTATUS Status = STATUS_SUCCESS; 31 PDEVICE_OBJECT DeviceObject = NULL; 32 33 DriverObject->DriverUnload = DriverUnload; 34 35 36 //ListTest();//双向链表 37 SingleListTest();//单链表 38 39 40 41 return Status; 42 } 43 44 45 void ListTest() 46 { 47 LIST_ENTRY ListHead; 48 PITEM Item = NULL; 49 50 ULONG i = 0; 51 52 //初始化链表 53 InitializeListHead(&ListHead); 54 55 for (i = 0; i < 10; i++) 56 { 57 Item = (PITEM) 58 ExAllocatePool(PagedPool, sizeof(ITEM)); 59 Item->ItemData = i; 60 InsertHeadList(&ListHead, &Item->u.ListEntry); 61 } 62 63 while (!IsListEmpty(&ListHead)) 64 { 65 PLIST_ENTRY ListEntry = RemoveTailList(&ListHead); 66 67 DbgPrint("%d\r\n", ((PITEM)ListEntry)->ItemData); 68 ExFreePool((PITEM)ListEntry); 69 } 70 } 71 72 void SingleListTest() 73 { 74 SINGLE_LIST_ENTRY ListHead; 75 76 PITEM Item = NULL; 77 ULONG i = 0; 78 //初始化链表 79 InitializeListHead(&ListHead); 80 for (i = 0; i < 10; i++) 81 { 82 Item = (PITEM) 83 ExAllocatePool(PagedPool, sizeof(ITEM)); 84 Item->ItemData = i; 85 PushEntryList(&ListHead, &Item->u.SingleListEntry); 86 } 87 88 while (!IsListEmpty(&ListHead)) 89 { 90 PSINGLE_LIST_ENTRY ListEntry = PopEntryList(&ListHead); 91 92 DbgPrint("%d\n", ((PITEM)ListEntry)->ItemData); 93 ExFreePool((PITEM)ListEntry); 94 } 95 96 } 97 98 99 100 VOID DriverUnload(PDRIVER_OBJECT DriverObject) 101 { 102 DbgPrint("DriverUnload()\r\n"); 103 }
时间: 2024-10-09 17:59:30