⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lldrv.pas

📁 pipe类 pipe类 pipe类 pipe类 pipe类
💻 PAS
字号:
unit LLDrv;


interface

uses WinSVC, VirtMach, Windows;

type

  cLLDrv = class
  private
    function InstallDriver(var SchSCManager:SC_HANDLE; DriverName, ServiceExe:PChar):boolean;
    function StartDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
    function StopDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
    function RemoveDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
    function OpenDevice(DriverName:PChar; var hDevice:THandle):boolean;
  public
    m_hDriver: THandle;

    constructor Create;
    destructor Destroy; override;
    
    function LoadDeviceDriver(DrName, Path:PChar):boolean;
    function UnLoadDeviceDriver(DrName:PChar):boolean;
  end;

implementation

constructor cLLDrv.Create;
begin
  inherited Create;
  m_hDriver:=INVALID_HANDLE_VALUE;
end;

destructor cLLDrv.Destroy;
begin
  inherited Destroy;
end;

function cLLDrv.InstallDriver(var SchSCManager:SC_HANDLE; DriverName, ServiceExe:PChar):boolean;
var
  schService: SC_HANDLE;
begin
    schService := CreateService(SchSCManager,          // SCManager database
                                DriverName,           // name of service
                                DriverName,           // name to display
                                SERVICE_ALL_ACCESS,    // desired access
                                SERVICE_KERNEL_DRIVER, // service type
                                SERVICE_DEMAND_START,  // start type
                                SERVICE_ERROR_NORMAL,  // error control type
                                ServiceExe,     // service's binary
                                nil,                  // no load ordering group
                                nil,                  // no tag identifier
                                nil,                  // no dependencies
                                nil,                  // LocalSystem account
                                nil                   // no password
                                );

    if (schService=0) then
                          begin
                            result:=false;
                            exit;
                          end;
    CloseServiceHandle( schService );
    result:=true;
end;

function cLLDrv.StartDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
var
  schService: SC_HANDLE;
  b:PChar;
begin
    schService:=OpenService(SchSCManager,
                              DriverName,
                              SERVICE_ALL_ACCESS
                            );
    if (schService=0) then
                         begin
                           result:=false;
                           exit;
                         end;
    b:=nil;
    result:=StartService(schService, 0, b);
    SetLastError(ERROR_SERVICE_ALREADY_RUNNING);
    CloseServiceHandle(schService);
end;

function cLLDrv.StopDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
var
  schService: SC_HANDLE;
  ServiceStatus: TServiceStatus;
begin
    schService := OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS);
    if (schService=0) then
                        begin
                          result:=false;
                          exit;
                        end;
    result:=ControlService(schService, SERVICE_CONTROL_STOP, ServiceStatus);
    CloseServiceHandle(schService);
end;

function cLLDrv.RemoveDriver(var SchSCManager:SC_HANDLE; DriverName:PChar):boolean;
var
  schService: SC_HANDLE;
begin
      schService:=OpenService( SchSCManager,
                               DriverName,
                               SERVICE_ALL_ACCESS
                              );
      if (schService=0) then
                          begin
                            result:=false;
                            exit;
                          end;
      result:=DeleteService(schService);
      CloseServiceHandle(schService);
end;

function cLLDrv.OpenDevice(DriverName:PChar; var hDevice:THandle):boolean;
begin
  hDevice := CreateFile(PChar('\\.\'+DriverName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hDevice=INVALID_HANDLE_VALUE) or (hDevice=0) then result:=false
                                                       else result:=true;
end;

function cLLDrv.LoadDeviceDriver(DrName, Path:PChar):boolean;
var
  schSCManager: SC_HANDLE;
begin
  result:=false;
  //if IsVMwarePresent then exit;
  if IsVPCPresent then exit;

  schSCManager:=OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);

	RemoveDriver(schSCManager, DrName); 	// Remove previous instance

	InstallDriver(schSCManager, DrName, Path); 	// Ignore success of installation: it may already be installed

	StartDriver(schSCManager, DrName); //Ignore success of start: it may already be started.

	result:=OpenDevice(DrName, m_hDriver);

 	CloseServiceHandle(schSCManager);
end;

function cLLDrv.UnLoadDeviceDriver(DrName:PChar):boolean;
var
  schSCManager: SC_HANDLE;
begin
  result:=StopDriver(schSCManager, DrName);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -