鱼C论坛

 找回密码
 立即注册
查看: 2680|回复: 2

用Delphi编写加载和卸载驱动sys代码!

[复制链接]
发表于 2013-11-4 14:34:32 | 显示全部楼层 |阅读模式
25鱼币
希望获得用Delphi编写加载和卸载驱动sys源代码!

最佳答案

查看完整内容

帮你找到了一篇源码,希望可以帮到你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-11-4 14:34:33 | 显示全部楼层
帮你找到了一篇源码,希望可以帮到你
  1. *********************************************************************** }
  2. { 模块名称:MyDriver                                                         }
  3. { 模块功能:加载/卸载驱动程序                                                }
  4. { 版 本号:v1.0.0                                                           }
  5. { 日      期:2008-5-18                                                        }
  6. { *********************************************************************** }
  7. unit MyDriver;

  8. interface

  9. uses
  10.     Windows,SysUtils,Tlhelp32,WinSvc;
  11.     {功能:加载驱动程序
  12.      参数:sztheDriverName:驱动程序完成路径.
  13.           szSvrName        :驱动程序名称.}
  14.     function InstallDriver(sztheDriverName,szSvrName:string):Boolean;
  15.     {功能:卸载驱动程序
  16.      参数:szSvrName        :驱动程序名称.}
  17.     function UnInstallDriver(szSvrName:string):Boolean;
  18.    
  19. implementation

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

  31.     if hServiceMgr=0 then
  32.       begin
  33.         {OpenSCManager() Faild.}
  34.         Result:=False;
  35.         Exit;
  36.       end;

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

  46.     if hServiceTwdm=0 then
  47.       begin
  48.         if GetLastError()=ERROR_SERVICE_EXISTS then
  49.           begin
  50.             {Service Exists}
  51.             hServiceTwdm:=OpenService(hServiceMgr,PChar(szSvrName),SERVICE_ALL_ACCESS);
  52.             if hServiceTwdm=0 then
  53.               begin
  54.                 CloseServiceHandle(hServiceMgr);
  55.                 Result:=False;
  56.                 Exit;
  57.               end;
  58.           end
  59.         else
  60.           begin
  61.             CloseServiceHandle(hServiceMgr);
  62.             Result:=False;
  63.             Exit;
  64.           end;
  65.       end;

  66.     {Start the drivers}
  67.     if hServiceTwdm<>0 then
  68.       begin
  69.         if StartService(hServiceTwdm,0,p)=False then
  70.           begin
  71.             if ERROR_SERVICE_ALREADY_RUNNING=GetLastError() then
  72.               begin
  73.                 {no real problem}
  74.               end
  75.             else
  76.               begin
  77.                 CloseServiceHandle(hServiceMgr);
  78.                 CloseServiceHandle(hServiceTwdm);
  79.                 Result:=False;
  80.                 Exit;
  81.               end;
  82.           end;

  83.         CloseServiceHandle(hServiceMgr);
  84.         CloseServiceHandle(hServiceTwdm);
  85.       end;
  86.      
  87.     Result:=True;
  88. end;

  89. function UnInstallDriver(szSvrName:string):Boolean;
  90. var
  91.     hServiceMgr,hServiceTwdm:SC_HANDLE;
  92.     SvrSta:SERVICE_STATUS;
  93. begin
  94.     hServiceMgr:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS );
  95.     if hServiceMgr=0 then
  96.       begin
  97.         {OpenSCManager() Faild.}
  98.         Result:=False;
  99.         Exit;
  100.       end;

  101.     hServiceTwdm:=OpenService(hServiceMgr,PChar(szSvrName),SERVICE_ALL_ACCESS );
  102.     if hServiceTwdm=0 then
  103.       begin
  104.         {OpenService() Faild.}
  105.         CloseServiceHandle(hServiceMgr);
  106.         Result:=False;
  107.         Exit;
  108.       end;

  109.     {停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。}
  110.     if ControlService(hServiceTwdm,SERVICE_CONTROL_STOP,SvrSta)=False then
  111.       begin
  112.         {ControlService() Faild.}
  113.         CloseServiceHandle(hServiceTwdm);
  114.         CloseServiceHandle(hServiceMgr);
  115.         Result:=False;
  116.         Exit;
  117.       end;
  118.     {动态卸载驱动程序.}
  119.     if DeleteService(hServiceTwdm)=False then
  120.       begin
  121.         {DeleteSrevice() Faild.}
  122.         CloseServiceHandle(hServiceTwdm);
  123.         CloseServiceHandle(hServiceMgr);
  124.         Result:=False;
  125.         Exit;
  126.       end;

  127.     CloseServiceHandle(hServiceTwdm);
  128.     CloseServiceHandle(hServiceMgr);
  129.     Result:=True;
  130. end;

  131. end.
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-11-6 01:33:07 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-25 10:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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