Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1547148
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2014-04-28 15:57:24


  1. //hello_nt.h
  2. #pragma once


  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. #include <ntddk.h>

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. #define PAGEDCODE code_seg("PAGE")
  11. #define LOCKEDCODE code_seg()
  12. #define INITCODE code_seg("INIT")

  13. #define PAGEDDATA data_seg("PAGE")
  14. #define LOCKEDDATA data_seg()
  15. #define INITDATA data_seg("INIT")


  16. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))

  17. typedef struct _DEVICE_EXTENSION {
  18.     PDEVICE_OBJECT pDevObj;
  19.     UNICODE_STRING ustrDeviceName; /* 设备名称 */
  20.     UNICODE_STRING ustrSymLinkName; /* 链接名称 */
  21. }DEVICE_EXTENSION, *PDEVICE_EXTENSION;

  22. NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject);
  23. VOID HelloDDKUnload(IN PDRIVER_OBJECT pDriverObject);
  24. NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj,
  25.                                     IN PIRP pIrp)


  1. //hello_nt.c
  2. #include "hello_nt.h"

  3. #define HELLO_DDK_DEVICE_NAME L"\\Device\\DriverDeviceName"
  4. #define HELLO_DDK_DOS_NAME L"\\??\\DriverDosName"

  5. #define DEBUG_ENTER() KdPrint(("%s entry.\n", __FUNCTION__));

  6. /*
  7. VOID WINAPI RtlInitUnicodeString(
  8.   _Inout_ PUNICODE_STRING DestinationString,
  9.   _In_opt_ PCWSTR SourceString
  10. );

  11. NTSTATUS IoCreateDevice(
  12.   _In_ PDRIVER_OBJECT DriverObject,
  13.   _In_ ULONG DeviceExtensionSize,
  14.   _In_opt_ PUNICODE_STRING DeviceName,
  15.   _In_ DEVICE_TYPE DeviceType,
  16.   _In_ ULONG DeviceCharacteristics,
  17.   _In_ BOOLEAN Exclusive,
  18.   _Out_ PDEVICE_OBJECT *DeviceObject
  19. );
  20. http://msdn.microsoft.com/en-us/library/windows/hardware/ff548397%28v=vs.85%29.aspx

  21.  */

  22. #pragma INITCODE
  23. NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject)
  24. {
  25.     NTSTATUS status;
  26.     PDEVICE_OBJECT pDevObj;
  27.     PDEVICE_EXTENSION pDevExt;
  28.     UNICODE_STRING devName;
  29.     UNICODE_STRING SymLinkName;

  30.     RtlInitUnicodeString(&devName, HELLO_DDK_DEVICE_NAME);
  31.     RtlInitUnicodeString(&SymLinkName, HELLO_DDK_DOS_NAME);

  32.     //创建设备,将驱动对象和设备对象绑定,指明设备的类型,名称等属性
  33.     status = IoCreateDevice(pDriverObject,
  34.                             sizeof(DEVICE_EXTENSION),
  35.                             &devName,
  36.                             FILE_DEVICE_UNKNOWN,
  37.                             FILE_DEVICE_SECURE_OPEN,
  38.                             FALSE,
  39.                             &pDevObj);

  40.     if ( !NT_SUCCESS(status) ) {
  41.         KdPrint(("IoCreateDevice err"));
  42.         return status;
  43.     }

  44.     pDevObj->Flags |= DO_BUFFERED_IO;
  45.     pDevExt = pDevObj->DeviceExtension;
  46.     pDevExt->pDevObj = pDevObj;
  47.     pDevExt->ustrDeviceName = devName;
  48.     pDevExt->ustrSymLinkName = SymLinkName;

  49.     //创建设备名称的符号链接,
  50.     status = IoCreateSymbolicLink(&SymLinkName, &devName);
  51.     if ( !NT_SUCCESS(status) ) {
  52.         KdPrint(("IoCreateSymbolicLink err"));
  53.         return status;
  54.     }

  55.     return STATUS_SUCCESS;
  56. }

  57. /* 删除CreateDevice创造的东西 */
  58. #pragma PAGEDCODE
  59. VOID HelloDDKUnload(IN PDRIVER_OBJECT pDriverObject)
  60. {
  61.     PDEVICE_OBJECT pDevObj;
  62.     PDEVICE_EXTENSION pDevExt;
  63.     DEBUG_ENTER();

  64.     //DriverObject->DeviceObject->DeviceExtension
  65.     pDevObj = pDriverObject->DeviceObject; //设备对象和驱动对象都有指向对方的指针
  66.     pDevExt = pDevObj->DeviceExtension;

  67.     //删除符号链接及设备对象
  68.     IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName);
  69.     IoDeleteDevice(pDevExt->pDevObj);
  70. }

  71. /*

  72. VOID IoCompleteRequest(
  73.   _In_ PIRP Irp,
  74.   _In_ CCHAR PriorityBoost
  75. );
  76. http://msdn.microsoft.com/en-us/library/ff548343%28VS.85%29.aspx
  77.  */
  78. #pragma PAGEDCODE
  79. NTSTATUS HelloDDKDispatchRoutine(IN PDEVICE_OBJECT pDevObj,
  80.                                     IN PIRP pIrp)
  81. {
  82.     NTSTATUS status = STATUS_SUCCESS;

  83.     DEBUG_ENTER();

  84.     pIrp->IoStatus.Status = status;
  85.     pIrp->IoStatus.Information = 0;
  86.     IoCompleteRequest(pIrp, IO_NO_INCREMENT);//马上完成了IO请求

  87.     return status;
  88. }

  89. /*
  90.  *驱动入口函数
  91.  */
  92. #pragma INITCODE
  93. //extern "C" NTSTATUS DriverEntry(IN PDEVICE_OBJECT pDriverObject,//c++程序
  94. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,
  95.                                 IN PUNICODE_STRING pRegistryPath)
  96. {
  97.     NTSTATUS status;
  98.     DEBUG_ENTER();

  99.     pDriverObject->DriverUnload = HelloDDKUnload;
  100.     pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKDispatchRoutine;
  101.     pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKDispatchRoutine;
  102.     pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKDispatchRoutine;
  103.     pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKDispatchRoutine;

  104.     status = CreateDevice(pDriverObject);
  105.     return status;
  106. }


  1. //hello_nt.inf
  2. ;; The Win2K DDK documentation contains an excellent INF reference.

  3. ;目标安装文件: abc.sys, (ABCkeyboard.sys)
  4. ;类子键:使用自定义的设备类,Class=ZhangfanDevice
  5. ;硬件子键:
  6. ;服务子键:HelloWDM,最好跟abc.sys的名字一致,也就是改为abc

  7. ;--------- Version Section ---------------------------------------------------

  8. [Version]
  9. Signature="$CHICAGO$"
  10. Provider=Zhangfan_Device2    ;驱动属性-驱动程序-驱动程序提供商
  11. DriverVer=11/1/2007,3.0.0.3    ;驱动程序版本

  12. ;###类子键,设备从属的设备类:
  13. ;HK_LM\SYSTEM\CurrentControlSet\Control\Class\{GUID}\Class

  14. ;如果是标准的设备,则使用相同的名字和GUID,否则创建自己的设备类和GUID
  15. Class=ZhangfanDevice    ;HK_LM\SYSTEM\CurrentControlSet\Control\Class\{GUID}\Class
  16. ClassGUID={EF2962F0-0D55-4bff-B8AA-2221EE8A79B0}    ;上面的{GUID}


  17. ; 如果使用标准设备类,则不需要这些
  18. ;--------- ClassInstall/ClassInstall32 Section -------------------------------

  19. ; 9X Style    Windows 98
  20. [ClassInstall]
  21. Addreg=Class_AddReg

  22. ; NT Style    Windows 2000
  23. [ClassInstall32]
  24. Addreg=Class_AddReg

  25. [Class_AddReg]
  26. HKR,,,,%DeviceClassName%
  27. HKR,,Icon,,"-5"

  28. ;待安装文件的位置及描述
  29. ;--------- SourceDiskNames and SourceDiskFiles Section -----------------------

  30. ; These sections identify source disks and files for installation. They are
  31. ; shown here as an example, but commented out.

  32. [SourceDisksNames]
  33. 1 = "HelloWDM",Disk1,,

  34. [SourceDisksFiles]
  35. abc.sys = 1,,


  36. ;目标安装位置
  37. ;--------- DestinationDirs Section -------------------------------------------

  38. [DestinationDirs]
  39. YouMark_Files_Driver = 10,System32\Drivers


  40. ;##硬件子键,硬件实例
  41. ;HKEY_LOCAL_MACHINE\SYSTEM\CONTROLSET001\ENUM (\ROOT\UNKNOWN\0000)
  42. ;--------- Manufacturer and Models Sections ----------------------------------

  43. [Manufacturer]
  44. %MfgName%=Mfg0

  45. [Mfg0]

  46. ; PCI hardware Ids use the form
  47. ; PCI\VEN_aaaa&DEV_bbbb&SUBSYS_cccccccc&REV_dd
  48. ;改成你自己的ID
  49. %DeviceDesc%=YouMark_DDI, PCI\VEN_9999&DEV_9999



  50. ;服务子键,兼容以前的NT驱动,也就是可以用INF安装NT驱动
  51. ;;HK_LM\SYSTEM\CurrentControlSet\services 生成"HelloWDM"
  52. ;---------- DDInstall Sections -----------------------------------------------
  53. ; --------- Windows 9X -----------------

  54. ; Experimentation has shown that DDInstall root names greater than 19 characters
  55. ; cause problems in Windows 98

  56. [YouMark_DDI]
  57. CopyFiles=YouMark_Files_Driver
  58. AddReg=YouMark_9X_AddReg

  59. [YouMark_9X_AddReg]
  60. HKR,,DevLoader,,*ntkern
  61. HKR,,NTMPDriver,,abc.sys
  62. HKR, "Parameters", "BreakOnEntry", 0x00010001, 0


  63. ; --------- Windows NT -----------------
  64. ;

  65. [YouMark_DDI.NT]
  66. CopyFiles=YouMark_Files_Driver
  67. AddReg=YouMark_NT_AddReg

  68. [YouMark_DDI.NT.Services]
  69. Addservice = HelloWDM, 0x00000002, YouMark_AddService

  70. [YouMark_AddService]
  71. DisplayName = %SvcDesc%
  72. ServiceType = 1 ; SERVICE_KERNEL_DRIVER
  73. StartType = 3 ; SERVICE_DEMAND_START
  74. ErrorControl = 1 ; SERVICE_ERROR_NORMAL
  75. ServiceBinary = %10%\System32\Drivers\abc.sys

  76. [YouMark_NT_AddReg]
  77. HKLM, "System\CurrentControlSet\Services\HelloWDM\Parameters",\
  78. "BreakOnEntry", 0x00010001, 0


  79. ; --------- Files (common) -------------

  80. [YouMark_Files_Driver]
  81. abc.sys

  82. ;--------- Strings Section ---------------------------------------------------

  83. [Strings]
  84. ProviderName="Zhangfan.inc"
  85. MfgName="Zhangfan Soft"        ;驱动属性-常规-制造商
  86. DeviceDesc="Hello World NT!"    ;设备管理器显示的名字,例如"标准VGA图形适配器"
  87. DeviceClassName="Zhangfan_Device_class" ;;驱动属性-常规-设备类型
  88. SvcDesc="Zhangfan"

阅读(1321) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~