-
-
[旧帖]
[原创]驱动学习心得,高手飘过,菜鸟交流
0.00雪花
-
发表于:
2009-10-25 13:55
3379
-
[旧帖] [原创]驱动学习心得,高手飘过,菜鸟交流
0.00雪花
[LEFT][FONT=Times][SIZE=3][COLOR=#000000]NTSTATUS[/COLOR][/SIZE][/FONT]
[FONT=新宋体][COLOR=#000000]DriverEntry([/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] IN PDRIVER_OBJECT DriverObject,[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] IN PUNICODE_STRING RegistryPath[/COLOR][/FONT]
[LEFT][FONT=新宋体][COLOR=#000000])[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000]{[/COLOR][/FONT]
[COLOR=#000000][FONT=Times][SIZE=3] [/SIZE][/FONT][FONT=新宋体]NTSTATUS Status = STATUS_SUCCESS; [/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] UNICODE_STRING ntDeviceName; [/COLOR][COLOR=green]// [/COLOR][/FONT][COLOR=green][FONT=新宋体]驱动名称[/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] UNICODE_STRING dosDeviceName; [/COLOR][COLOR=green]//[/COLOR][/FONT][COLOR=green][FONT=新宋体]符号链接名[/FONT][/COLOR][/LEFT]
[FONT=新宋体][COLOR=#000000] PDEVICE_EXTENSION deviceExtension;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] PDEVICE_OBJECT deviceObject = NULL;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] KdPrint(([/COLOR][COLOR=maroon]"[NTddkDriver] DriverEntry: %wZ\n"[/COLOR][COLOR=#000000], RegistryPath));[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=green]//[/COLOR][/FONT][COLOR=green][FONT=新宋体]创建设备对象[/FONT][/COLOR]
[COLOR=#000000][FONT=新宋体] RtlInitUnicodeString(&ntDeviceName, L[/FONT][FONT=新宋体][FONT=Times]”[/FONT][/FONT][/COLOR][COLOR=maroon][FONT=新宋体]\\\\.\\Ntddk[/FONT][/COLOR][COLOR=#000000][FONT=新宋体][FONT=Times]”[/FONT][/FONT][FONT=新宋体]);[/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] Status = IoCreateDevice([/COLOR][/FONT][FONT=新宋体][COLOR=#000000]DriverObject,[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]sizeof[/COLOR][COLOR=#000000](DEVICE_EXTENSION),[/COLOR][COLOR=green]// DeviceExtension[/COLOR][/FONT][COLOR=green][FONT=新宋体]大小[/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] &ntDeviceName,[/COLOR][COLOR=green]// [/COLOR][/FONT][COLOR=green][FONT=新宋体]驱动名称[/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] FILE_DEVICE_UNKNOWN,[/COLOR][COLOR=green]// [/COLOR][/FONT][COLOR=green][FONT=新宋体]类型[/FONT][/COLOR]
[LEFT][FONT=新宋体][COLOR=#000000] 0, [/COLOR][/FONT][/LEFT]
[FONT=新宋体][COLOR=#000000][FONT=Verdana][COLOR=#333333] [/COLOR][/FONT] TRUE, [/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] &deviceObject[/COLOR][/FONT][FONT=新宋体][COLOR=#000000]);[/COLOR][/FONT]
[LEFT][FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]if[/COLOR][COLOR=#000000](!NT_SUCCESS(Status))[/COLOR][/FONT][/LEFT]
[FONT=新宋体][COLOR=#000000] {[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] KdPrint(([/COLOR][COLOR=maroon]"[NTddkDriver] IoCreateDevice Error Code = 0x%X\n"[/COLOR][COLOR=#000000], Status));[/COLOR][/FONT]
[LEFT][FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]return[/COLOR][COLOR=#000000] Status;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] }[/COLOR][/FONT][/LEFT]
[FONT=新宋体][COLOR=#000000] deviceExtension = (PDEVICE_EXTENSION)deviceObject->DeviceExtension;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=green]//[/COLOR][/FONT][COLOR=green][FONT=新宋体]创建符号链接,以便win32应用程序调用驱动[/FONT][/COLOR]
[LEFT][COLOR=#000000][FONT=新宋体] RtlInitUnicodeString(&dosDeviceName, L[/FONT][FONT=新宋体][FONT=Times]”[/FONT][/FONT][/COLOR][COLOR=maroon][FONT=新宋体] \\DosDevices\\Ntddk[/FONT][/COLOR][COLOR=#000000][FONT=新宋体][FONT=Times]”[/FONT][/FONT][FONT=新宋体]);[/FONT][/COLOR][/LEFT]
[FONT=新宋体][COLOR=#000000] Status = IoCreateSymbolicLink(&dosDeviceName, &ntDeviceName);[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]if[/COLOR][COLOR=#000000](!NT_SUCCESS(Status))[/COLOR][/FONT]
[LEFT][FONT=新宋体][COLOR=#000000] {[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] KdPrint(([/COLOR][COLOR=maroon]"[NTddkDriver] IoCreateSymbolicLink Error Code = 0x%X\n"[/COLOR][COLOR=#000000], Status));[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] IoDeleteDevice(deviceObject); [/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]return[/COLOR][COLOR=#000000] Status;[/COLOR][/FONT]
[FONT=Times][SIZE=3][COLOR=#000000] }[/COLOR][/SIZE][/FONT][/LEFT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=green]//[/COLOR][/FONT][COLOR=green][FONT=新宋体]关联派遣函数[/FONT][/COLOR]
[LEFT][FONT=新宋体][COLOR=#000000] DriverObject->MajorFunction[IRP_MJ_CREATE] = NtddkDispatchCreate;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] DriverObject->MajorFunction[IRP_MJ_CLOSE] = NtddkDispatchClose;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = NtddkDispatchDeviceControl;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] DriverObject->DriverUnload = NtddkUnload;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][COLOR=blue]return[/COLOR][COLOR=#000000] Status;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000]}[/COLOR][/FONT][/LEFT]
[/LEFT]
[LEFT][FONT=宋体][FONT=宋体][FONT=新宋体][COLOR=#000000]VOID[/COLOR][/FONT][/FONT][/FONT][FONT=宋体][FONT=宋体][/LEFT]
[FONT=新宋体][COLOR=#000000]NtddkUnload([/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] IN PDRIVER_OBJECT DriverObject[/COLOR][/FONT]
[LEFT][FONT=新宋体][COLOR=#000000])[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000]{[/COLOR][/FONT][/LEFT]
[LEFT][FONT=新宋体][COLOR=#000000] UNICODE_STRING dosDeviceName;[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000] [/COLOR][/FONT][COLOR=green][FONT=新宋体]// [/FONT][/COLOR][COLOR=green][FONT=新宋体]释放其他资源[/FONT][/COLOR][/LEFT]
[LEFT][COLOR=green][FONT=新宋体] //[/FONT][/COLOR][COLOR=green][FONT=新宋体]删除符号链接[/FONT][/COLOR]
[COLOR=#000000][FONT=新宋体] RtlInitUnicodeString(&dosDeviceName, L[/FONT][FONT=新宋体][FONT=Times]”[/FONT][/FONT][/COLOR][COLOR=maroon][FONT=新宋体] [URL="file://\\DosDevices\\Ntddk"]\\DosDevices\\Ntddk[/URL][/FONT][/COLOR][COLOR=#000000][FONT=新宋体][FONT=Times]”[/FONT][/FONT][FONT=新宋体]);[/FONT][/COLOR]
[FONT=新宋体][COLOR=#000000] IoDeleteSymbolicLink(&dosDeviceName);[/COLOR][/FONT][/LEFT]
[COLOR=green][FONT=新宋体] //[/FONT][/COLOR][COLOR=green][FONT=新宋体]删除设备对象[/FONT][/COLOR]
[LEFT][FONT=新宋体][COLOR=#000000] IoDeleteDevice(DriverObject->DeviceObject);[/COLOR][/FONT][/LEFT]
[LEFT][FONT=新宋体][COLOR=#000000] KdPrint(([/COLOR][COLOR=maroon]"[NTddkDriver] Unloaded"[/COLOR][COLOR=#000000]));[/COLOR][/FONT]
[FONT=新宋体][COLOR=#000000]}[/COLOR][/FONT][/LEFT]
[/FONT][/FONT]
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!