驱动动态加载

function InstallDriver(sztheDriverName,szSvrName:string):Boolean;
var
    hServiceMgr,hServiceTwdm:SC_HANDLE;
    szDir:array[0..1023]of char;
    lpsztheDriverName,p:PChar;
begin
    ZeroMemory(@szDir,1024);
    strcopy(szDir,Pchar(sztheDriverName));
    lpsztheDriverName:=@szDir;
    {打开服务控制管理器}
    hServiceMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS );

    if hServiceMgr=0 then
      begin
        {OpenSCManager() Faild.}
        Result:=False;
        Exit;
      end;

    hServiceTwdm:=CreateService(hServiceMgr,
                                PChar(szSvrName),       {SYSTEM/CurrentControlSet/Services驱动程序的在注册表中的名字}
                                PChar(szSvrName),       {注册表驱动程序的 DisplayName 值}
                                SERVICE_ALL_ACCESS,     {加载驱动程序的访问权限}
                                SERVICE_KERNEL_DRIVER,{表示加载的服务是驱动程序}
                                SERVICE_DEMAND_START, {注册表驱动程序的 Start 值}
                                SERVICE_ERROR_IGNORE, {注册表驱动程序的 ErrorControl 值}
                                lpsztheDriverName,      {注册表驱动程序的 ImagePath 值}
                                nil,nil,nil,nil,nil);

    if hServiceTwdm=0 then
      begin
        if GetLastError()=ERROR_SERVICE_EXISTS then
          begin
            {Service Exists}
            hServiceTwdm:=OpenService(hServiceMgr,PChar(szSvrName),SERVICE_ALL_ACCESS);
            if hServiceTwdm=0 then
              begin
                CloseServiceHandle(hServiceMgr);
                Result:=False;
                Exit;
              end;
          end
        else
          begin
            CloseServiceHandle(hServiceMgr);
            Result:=False;
            Exit;
          end;
      end;

    {Start the drivers}
    if hServiceTwdm<>0 then
      begin
        if StartService(hServiceTwdm,0,p)=False then
          begin
            if ERROR_SERVICE_ALREADY_RUNNING=GetLastError() then
              begin
                {no real problem}
              end
            else
              begin
                CloseServiceHandle(hServiceMgr);
                CloseServiceHandle(hServiceTwdm);
                Result:=False;
                Exit;
              end;
          end;

        CloseServiceHandle(hServiceMgr);
        CloseServiceHandle(hServiceTwdm);
      end;

    Result:=True;
end;

function UnInstallDriver(szSvrName:string):Boolean;
var
    hServiceMgr,hServiceTwdm:SC_HANDLE;
    SvrSta:SERVICE_STATUS;
begin
    hServiceMgr:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS );
    if hServiceMgr=0 then
      begin
        {OpenSCManager() Faild.}
        Result:=False;
        Exit;
      end;

    hServiceTwdm:=OpenService(hServiceMgr,PChar(szSvrName),SERVICE_ALL_ACCESS );
    if hServiceTwdm=0 then
      begin
        {OpenService() Faild.}
        CloseServiceHandle(hServiceMgr);
        Result:=False;
        Exit;
      end;

    {停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。}
    if ControlService(hServiceTwdm,SERVICE_CONTROL_STOP,SvrSta)=False then
      begin
        {ControlService() Faild.}
        CloseServiceHandle(hServiceTwdm);
        CloseServiceHandle(hServiceMgr);
        Result:=False;
        Exit;
      end;
    {动态卸载驱动程序.}
    if DeleteService(hServiceTwdm)=False then
      begin
        {DeleteSrevice() Faild.}
        CloseServiceHandle(hServiceTwdm);
        CloseServiceHandle(hServiceMgr);
        Result:=False;
        Exit;
      end;

    CloseServiceHandle(hServiceTwdm);
    CloseServiceHandle(hServiceMgr);
    Result:=True;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
InstallDriver('G:/FileInterceptor.sys','FileInXX');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnInstallDriver('FileInXX');
end;

 

你可能感兴趣的:(驱动动态加载)