网上看到一个可以加载驱动的源码。。。收集起来。。。
Code
*********************************************************************** }
{ 模块名称:MyDriver }
{ 模块功能:加载/卸载驱动程序 }
{ 版 本号:v1.0.0 }
{ 日 期:2008-5-18 }
{ *********************************************************************** }
unit MyDriver;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
interface
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
uses
Windows,SysUtils,Tlhelp32,WinSvc;
{功能:加载驱动程序
参数:sztheDriverName:驱动程序完成路径.
szSvrName :驱动程序名称.}
function InstallDriver(sztheDriverName,szSvrName:string):Boolean;
{功能:卸载驱动程序
参数:szSvrName :驱动程序名称.}
function UnInstallDriver(szSvrName:string):Boolean;
implementation
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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 );
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if hServiceMgr=0 then
begin
{OpenSCManager() Faild.}
Result:=False;
Exit;
end;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
{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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CloseServiceHandle(hServiceMgr);
CloseServiceHandle(hServiceTwdm);
end;
Result:=True;
end;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
hServiceTwdm:=OpenService(hServiceMgr,PChar(szSvrName),SERVICE_ALL_ACCESS );
if hServiceTwdm=0 then
begin
{OpenService() Faild.}
CloseServiceHandle(hServiceMgr);
Result:=False;
Exit;
end;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
{停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。}
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CloseServiceHandle(hServiceTwdm);
CloseServiceHandle(hServiceMgr);
Result:=True;
end;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
end.