找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 5484|回复: 2

[分享] 【分享】本人写的一个基于C#的驱动管理类

[复制链接]

2

主题

12

回帖

0

精华

铜牌会员

积分
217
发表于 2016-8-27 16:02:30 | 显示全部楼层 |阅读模式
本帖最后由 JerryAJ 于 2016-8-27 16:04 编辑

最近打算用C#做界面,突发奇想就弄了这个驱动管理类,原理和c++那些没什么不同,步骤还是老样子,只是有些地方做了修改
小生也是刚入门,技术有限O(∩_∩)O
目前在WIN7 x64和WIN10 1024 x64下测试通过

具体使用方法,我会上传一个项目文件,里面关于这个类的使用和驱动通信有一个实例

说明:里面的NativeApi和NativeApiEx 2个类是用于声明需要用到的API函数和相关常量


贴上类的主要代码:
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using System.IO;
  4. using System.ServiceProcess;
  5. /*
  6.     .Net driver manager class template
  7.     Version:1.0
  8.     Autor:JerryAJ
  9. */
  10. namespace KernelBox
  11. {
  12.     public class DriverManager
  13.     {
  14.         // 驱动文件的名称
  15.         public static string g_strSysName = "MainDriver.sys";
  16.         // 驱动文件的路径
  17.         public static string g_strSysPath = Directory.GetCurrentDirectory() + "\" + g_strSysName;
  18.         // 驱动符号链接名称
  19.         public static string g_strSysLinkName = "\\\\.\\AJ_Driver"; //格式:\\\\.\\xxoo
  20.         // 驱动服务名称
  21.         public static string g_strSysSvcLinkName = "AJ_Driver";
  22.         // 驱动句柄
  23.         public SafeFileHandle hDrv;
  24.         // SCM句柄
  25.         private IntPtr hSCManager;
  26.         // 驱动服务句柄
  27.         private IntPtr hService;
  28.         

  29.         // 获取驱动服务句柄
  30.         public bool GetSvcHandle()
  31.         {
  32.             hSCManager = NativeApi.OpenSCManager(null, null, (uint)NativeApiEx.SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
  33.             if (IntPtr.Zero == hSCManager)
  34.                 return false;
  35.             hService = NativeApi.OpenService(hSCManager, g_strSysSvcLinkName, (uint)NativeApiEx.SERVICE_ACCESS.SERVICE_ALL_ACCESS);
  36.             if (IntPtr.Zero == hService)
  37.             {
  38.                 NativeApi.CloseServiceHandle(hService);
  39.                 return false;
  40.             }
  41.             return true;
  42.         }

  43.         // 安装驱动服务
  44.         public bool Install()
  45.         {
  46.             hSCManager = NativeApi.OpenSCManager(null, null, (uint)NativeApiEx.SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
  47.             if (IntPtr.Zero == hSCManager)
  48.                 return false;
  49.             hService = NativeApi.CreateService(hSCManager, g_strSysSvcLinkName, g_strSysSvcLinkName,
  50.                 (uint)NativeApiEx.SERVICE_ACCESS.SERVICE_ALL_ACCESS, (uint)NativeApiEx.SERVICE_TYPE.SERVICE_KERNEL_DRIVER,
  51.                 (uint)NativeApiEx.SERVICE_START.SERVICE_DEMAND_START, (uint)NativeApiEx.SERVICE_ERROR.SERVICE_ERROR_NORMAL,
  52.                 g_strSysPath, null, null, null, null, null);

  53.             if (IntPtr.Zero == hService)
  54.             {
  55.                 NativeApi.GetLastError();
  56.                 NativeApi.CloseServiceHandle(hService);
  57.                 return false;
  58.             }
  59.             return true;
  60.         }

  61.         // 启动驱动服务
  62.         public bool Start()
  63.         {
  64.             if (!NativeApi.StartService(hService, 0x0, null))
  65.                 return false;
  66.             return true;
  67.         }

  68.         // 停止驱动服务
  69.         public bool Stop()
  70.         {
  71.             try
  72.             {
  73.                 ServiceController service = new ServiceController(g_strSysSvcLinkName);
  74.                 if (service.Status == ServiceControllerStatus.Stopped)
  75.                     return true;
  76.                 else
  77.                 {
  78.                     TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
  79.                     service.Stop();
  80.                     service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  81.                 }
  82.             }
  83.             catch (Exception)
  84.             {
  85.                 return false;
  86.                 throw;
  87.             }
  88.             return true;
  89.         }

  90.         // 卸载驱动
  91.         public bool Remove()
  92.         {
  93.             if (!NativeApi.DeleteService(hService))
  94.                 return false;
  95.             else
  96.             {
  97.                 NativeApi.CloseServiceHandle(hService);
  98.                 NativeApi.CloseServiceHandle(hSCManager);
  99.             }
  100.             return true;
  101.         }

  102.         // 打开当前驱动
  103.         public SafeFileHandle OpenDriver(string strLinkName)
  104.         {
  105.             return NativeApi.CreateFile(strLinkName, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
  106.         }

  107.         // 用于驱动通信的IO控制器
  108.         public bool IoControl(SafeFileHandle hDriver, uint nIoCode, object InBuffer, uint nInBufferSize, object OutBuffer, uint nOutBufferSize, ref uint pBytesReturned, ref System.Threading.NativeOverlapped Overlapped)
  109.         {
  110.             const uint FILE_ANY_ACCESS = 0;
  111.             const uint METHOD_BUFFERED = 0;
  112.             bool bRet;
  113.             nIoCode = ((int)NativeApiEx.DEVICE_TYPE.FILE_DEVICE_UNKNOWN * 65536) | (FILE_ANY_ACCESS * 16384) | (nIoCode * 4) | METHOD_BUFFERED;
  114.             bRet = NativeApi.DeviceIoControl(hDriver, nIoCode, InBuffer, nInBufferSize, OutBuffer, nOutBufferSize, ref pBytesReturned, ref Overlapped);
  115.             return bRet;
  116.         }

  117.         // 初始化
  118.         public bool InitializeDriver()
  119.         {
  120.             bool bRet = false;
  121.             hDrv = OpenDriver(g_strSysLinkName);
  122.             NativeApi.GetLastError();
  123.             if (!hDrv.IsInvalid)
  124.                 bRet = GetSvcHandle();
  125.             else
  126.             {
  127.                 bRet = Install();
  128.                 if (!bRet)
  129.                     return bRet;
  130.                 else
  131.                 {
  132.                     bRet = Start();
  133.                     hDrv = OpenDriver(g_strSysLinkName);
  134.                 }
  135.             }
  136.             return bRet;
  137.         }

  138.         // 卸载驱动
  139.         public bool RemoveDrvier()
  140.         {
  141.             hDrv.Close();
  142.             hDrv.Dispose();
  143.             hDrv = null;

  144.             bool bRet = false;
  145.             bRet = Stop();
  146.             if (!bRet)
  147.                 return bRet;

  148.             bRet = Remove();

  149.             return bRet;
  150.         }

  151.     }
  152. }
复制代码


完毕!

.Net driver manager class.zip

27.72 KB, 下载次数: 3805

测试项目

评分

参与人数 1水晶币 +20 收起 理由
Tesla.Angela + 20 赞一个!

查看全部评分

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2016-8-27 23:17:19 | 显示全部楼层
写的不错,加分支持。

2

主题

12

回帖

0

精华

铜牌会员

积分
217
 楼主| 发表于 2016-8-28 11:33:49 | 显示全部楼层
谢谢TA大大支持
努力学习,天天向上
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

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