JerryAJ 发表于 2016-8-27 16:02:30

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

本帖最后由 JerryAJ 于 2016-8-27 16:04 编辑

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

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

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


贴上类的主要代码:
using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.ServiceProcess;
/*
    .Net driver manager class template
    Version:1.0
    Autor:JerryAJ
*/
namespace KernelBox
{
    public class DriverManager
    {
      // 驱动文件的名称
      public static string g_strSysName = "MainDriver.sys";
      // 驱动文件的路径
      public static string g_strSysPath = Directory.GetCurrentDirectory() + "\\" + g_strSysName;
      // 驱动符号链接名称
      public static string g_strSysLinkName = "\\\\.\\AJ_Driver"; //格式:\\\\.\\xxoo
      // 驱动服务名称
      public static string g_strSysSvcLinkName = "AJ_Driver";
      // 驱动句柄
      public SafeFileHandle hDrv;
      // SCM句柄
      private IntPtr hSCManager;
      // 驱动服务句柄
      private IntPtr hService;
      

      // 获取驱动服务句柄
      public bool GetSvcHandle()
      {
            hSCManager = NativeApi.OpenSCManager(null, null, (uint)NativeApiEx.SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
            if (IntPtr.Zero == hSCManager)
                return false;
            hService = NativeApi.OpenService(hSCManager, g_strSysSvcLinkName, (uint)NativeApiEx.SERVICE_ACCESS.SERVICE_ALL_ACCESS);
            if (IntPtr.Zero == hService)
            {
                NativeApi.CloseServiceHandle(hService);
                return false;
            }
            return true;
      }

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

            if (IntPtr.Zero == hService)
            {
                NativeApi.GetLastError();
                NativeApi.CloseServiceHandle(hService);
                return false;
            }
            return true;
      }

      // 启动驱动服务
      public bool Start()
      {
            if (!NativeApi.StartService(hService, 0x0, null))
                return false;
            return true;
      }

      // 停止驱动服务
      public bool Stop()
      {
            try
            {
                ServiceController service = new ServiceController(g_strSysSvcLinkName);
                if (service.Status == ServiceControllerStatus.Stopped)
                  return true;
                else
                {
                  TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
                  service.Stop();
                  service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
            return true;
      }

      // 卸载驱动
      public bool Remove()
      {
            if (!NativeApi.DeleteService(hService))
                return false;
            else
            {
                NativeApi.CloseServiceHandle(hService);
                NativeApi.CloseServiceHandle(hSCManager);
            }
            return true;
      }

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

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

      // 初始化
      public bool InitializeDriver()
      {
            bool bRet = false;
            hDrv = OpenDriver(g_strSysLinkName);
            NativeApi.GetLastError();
            if (!hDrv.IsInvalid)
                bRet = GetSvcHandle();
            else
            {
                bRet = Install();
                if (!bRet)
                  return bRet;
                else
                {
                  bRet = Start();
                  hDrv = OpenDriver(g_strSysLinkName);
                }
            }
            return bRet;
      }

      // 卸载驱动
      public bool RemoveDrvier()
      {
            hDrv.Close();
            hDrv.Dispose();
            hDrv = null;

            bool bRet = false;
            bRet = Stop();
            if (!bRet)
                return bRet;

            bRet = Remove();

            return bRet;
      }

    }
}

完毕!

Tesla.Angela 发表于 2016-8-27 23:17:19

写的不错,加分支持。

JerryAJ 发表于 2016-8-28 11:33:49

谢谢TA大大支持
努力学习,天天向上
页: [1]
查看完整版本: 【分享】本人写的一个基于C#的驱动管理类