delphi加载驱动的代码演示

网上看到一个可以加载驱动的源码。。。收集起来。。。

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
*********************************************************************** }
{ 模块名称:MyDriver                                                         }
{ 模块功能:加载/卸载驱动程序                                                }
{ 版 本号:v1.0.0                                                           }
{ 日      期:2008-5-18                                                        }
{ *********************************************************************** }
unit MyDriver;

interface

uses
    Windows,SysUtils,Tlhelp32,WinSvc;
    
{功能:加载驱动程序
     参数:sztheDriverName:驱动程序完成路径.
          szSvrName        :驱动程序名称.
}
    
function InstallDriver(sztheDriverName,szSvrName:string):Boolean;
    
{功能:卸载驱动程序
     参数:szSvrName        :驱动程序名称.
}
    
function UnInstallDriver(szSvrName:string):Boolean;
   
implementation

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(nilnil, 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;

end.

转载于:https://www.cnblogs.com/iinsnian/archive/2009/04/28/1444968.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值