看流星社区

 找回密码
 注册账号
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
零基础辅助入门教学 原创 高清 专业课程售后(每日解答)
赞助广告位 请点击这里联系站长 QQ20209081
赞助广告位 请点击这里联系站长 QQ20209081
赞助广告位 请点击这里联系站长 QQ20209081
查看: 493|回复: 5

VC驱动调用驱动源码

[复制链接]

该用户从未签到

发表于 2019-12-25 13:21:31 | 显示全部楼层 |阅读模式


  1. #pragma once
  2. //DriverA
  3. #include <ntddk.h>

  4. //创建自定义设备扩展
  5. typedef struct _DEVICE_EXTENTION
  6. {
  7.     PDEVICE_OBJECT pDeviceObject;
  8.     UNICODE_STRING uszDeviceName;
  9.     UNICODE_STRING uszSymbolName;
  10.     PIRP pCurrentIrp;
  11.     KTIMER kTimer;
  12.     KDPC kDpc;
  13. }DEVICE_EXTENTION, *PDEVICE_EXTENTION;

  14. //驱动入口
  15. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName);
  16. //驱动卸载
  17. VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject);
  18. //创建设备对象 为了交互数据
  19. NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject);
  20. //普通回调
  21. NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
  22. //读取回调 将当前要处理的IRP挂起 3秒后触发DPC回调结束IRP 同步
  23. NTSTATUS DispatchRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);

  24. #include "DriverA.h"

  25. //DPC回调
  26. VOID DeferTimer(
  27.     _In_     struct _KDPC *Dpc,
  28.     _In_opt_ PVOID        DeferredContext,
  29.     _In_opt_ PVOID        SystemArgument1,
  30.     _In_opt_ PVOID        SystemArgument2
  31. )
  32. {
  33.     PDEVICE_EXTENTION pDeviceExtention = NULL;
  34.     PDEVICE_OBJECT pDeviceObject = (PDEVICE_OBJECT)DeferredContext;
  35.     pDeviceExtention = (PDEVICE_EXTENTION)pDeviceObject->DeviceExtension;
  36.     PIRP pCurrentIrp = pDeviceExtention->pCurrentIrp;
  37.     KdPrint(("DriverA\n"));
  38.     pCurrentIrp->IoStatus.Information = 0;
  39.     pCurrentIrp->IoStatus.Status = STATUS_SUCCESS;
  40.     IoCompleteRequest(pCurrentIrp, IO_NO_INCREMENT);
  41. }

  42. //驱动卸载
  43. VOID MyDriverUnload(PDRIVER_OBJECT pDriverObject)
  44. {
  45.     PDEVICE_EXTENTION pDeviceExtention = NULL;
  46.     PDEVICE_OBJECT pFirstDeviceObject = NULL;
  47.     pFirstDeviceObject = pDriverObject->DeviceObject;
  48.     ASSERT(pFirstDeviceObject != NULL);
  49.     pDeviceExtention = (PDEVICE_EXTENTION)(pFirstDeviceObject->DeviceExtension);
  50.     KeCancelTimer(&pDeviceExtention->kTimer);
  51.     pDeviceExtention = (PDEVICE_EXTENTION)pFirstDeviceObject->DeviceExtension;
  52.     IoDeleteSymbolicLink(&pDeviceExtention->uszSymbolName);
  53.     IoDeleteDevice(pDeviceExtention->pDeviceObject);
  54. }

  55. //创建设备对象 为了交互数据
  56. NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject)
  57. {
  58.     NTSTATUS ntStatus = STATUS_SUCCESS;
  59.     UNICODE_STRING uszDeviceName = RTL_CONSTANT_STRING(L"\\Device\\DriverA");
  60.     UNICODE_STRING uszSymbolName = RTL_CONSTANT_STRING(L"\\??\\DriverASymbol");
  61.     PDEVICE_OBJECT pDeviceObject = NULL;
  62.     PDEVICE_EXTENTION pDeviceExtention = NULL;

  63.     ntStatus = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENTION), &uszDeviceName,
  64.         FILE_DEVICE_UNKNOWN, 0, TRUE, &pDeviceObject);
  65.     if (!NT_SUCCESS(ntStatus))
  66.     {
  67.         KdPrint(("IoCreateDevice 错误:%x\n", ntStatus));
  68.         return ntStatus;
  69.     }
  70.     ntStatus = IoCreateSymbolicLink(&uszSymbolName, &uszDeviceName);
  71.     if (!NT_SUCCESS(ntStatus))
  72.     {
  73.         KdPrint(("IoCreateSymbolicLink 错误:%x\n", ntStatus));
  74.         return ntStatus;
  75.     }
  76.     pDeviceExtention = (PDEVICE_EXTENTION)(pDeviceObject->DeviceExtension);
  77.     //初始化时间对象
  78.     KeInitializeTimer(&pDeviceExtention->kTimer);
  79.     //初始化DPC对象
  80.     KeInitializeDpc(&pDeviceExtention->kDpc, DeferTimer, (PVOID)pDeviceObject);
  81.     pDeviceExtention->pDeviceObject = pDeviceObject;
  82.     pDeviceExtention->uszDeviceName = uszDeviceName;
  83.     pDeviceExtention->uszSymbolName = uszSymbolName;
  84.     pDeviceObject->Flags |= DO_DIRECT_IO;

  85.     return ntStatus;
  86. }

  87. //普通回调
  88. NTSTATUS DispatchGeneral(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
  89. {
  90.     NTSTATUS ntStatus = STATUS_SUCCESS;
  91.     pIrp->IoStatus.Information = 0;
  92.     pIrp->IoStatus.Status = ntStatus;
  93.     IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  94.     return ntStatus;
  95. }

  96. //读取回调 将当前要处理的IRP挂起 3秒后触发DPC回调结束IRP 同步
  97. NTSTATUS DispatchRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
  98. {
  99.     PDEVICE_EXTENTION pDeviceExtention = NULL;

  100.     IoMarkIrpPending(pIrp);

  101.     pDeviceExtention = (PDEVICE_EXTENTION)(pDeviceObject->DeviceExtension);
  102.     pDeviceExtention->pCurrentIrp = pIrp;
  103.     LARGE_INTEGER aa = { 0 };
  104.     aa.QuadPart = -30000000;
  105.     //启动定时器 3秒后进入DPC回调
  106.     KeSetTimer(
  107.         &pDeviceExtention->kTimer,
  108.         aa,
  109.         &pDeviceExtention->kDpc);
  110.     return STATUS_PENDING;
  111. }

  112. //驱动入口
  113. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING puszRegPathName)
  114. {
  115.     NTSTATUS ntStatus = STATUS_SUCCESS;
  116.     pDriverObject->DriverUnload = MyDriverUnload;
  117.     CreateDevice(pDriverObject);
  118.     for (ULONG64 uCount = 0; uCount < IRP_MJ_MAXIMUM_FUNCTION; uCount++)
  119.     {
  120.         pDriverObject->MajorFunction[uCount] = DispatchGeneral;
  121.     }
  122.     pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
  123.     return ntStatus;
  124. }

  125. #include <ntddk.h>
  126. //DriverB
  127. VOID DriverUnLoad(PDRIVER_OBJECT pDriverObj)
  128. {
  129.     KdPrint(("Driver unload\n"));
  130. }

  131. NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
  132. {
  133.     KdPrint(("Driver load\n"));
  134.     pDriverObj->DriverUnload = DriverUnLoad;
  135.     HANDLE hDeivce = NULL;
  136.     OBJECT_ATTRIBUTES objAttr = { 0 };
  137.     IO_STATUS_BLOCK ioStack = { 0 };
  138.     NTSTATUS ntStatus = 0;
  139.     UNICODE_STRING uszDeviceName = RTL_CONSTANT_STRING(L"\\Device\\DriverA");
  140.     InitializeObjectAttributes(&objAttr, &uszDeviceName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  141.     ntStatus = ZwCreateFile(&hDeivce, GENERIC_ALL, &objAttr, &ioStack, 0, FILE_ATTRIBUTE_NORMAL, FILE_READ_ACCESS, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_ALERT, NULL, 0);
  142.     if (!NT_SUCCESS(ntStatus))
  143.     {
  144.         KdPrint(("ZwCreateFile 错误\n"));
  145.         return ntStatus;
  146.     }
  147.     ntStatus = ZwReadFile(hDeivce, NULL, NULL, NULL, &ioStack, NULL, 0, 0, NULL);
  148.     if (!NT_SUCCESS(ntStatus))
  149.     {
  150.         KdPrint(("ZwReadFile 错误\n"));
  151.         ZwClose(hDeivce);
  152.         return ntStatus;
  153.     }
  154.     ZwClose(hDeivce);
  155.     KdPrint(("DriverB\n"));
  156.     return STATUS_SUCCESS;
  157. }
复制代码

该用户从未签到

发表于 2020-1-2 18:41:16 | 显示全部楼层
sadfsdafewqrwerweasdfdaf

该用户从未签到

发表于 2020-1-6 13:36:20 | 显示全部楼层
支持楼主,支持看流星社区,以后我会经常来!
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2020-8-6 09:53

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表