delphi加载驱动的代码演示


本文整理自网络,侵删。

 *********************************************************************** }
{ 模块名称: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(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;

end.

相关阅读 >>

Delphi 进程隐藏的若干方法

学习使用Delphi for android 调用java类库

Delphi 实现程序 动态 类名

Delphi firemonkey app 如何获得当前系统语言设置

Delphi 写变速齿轮外挂

Delphi 串口控制继电器

Delphi listview用法

Delphi xe7实现获取程序是否已管理员模式运行以及运行的windows用户名

Delphi 测试字符串写入类: tstringwriter

Delphi xe将图标和图像添加为资源

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...