3310|2

80

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

驱动加载报错“找不到指定文件”?急急急 [复制链接]

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.

最新回复

VB?過來,幫忙頂下》。。   详情 回复 发表于 2010-1-11 22:53
点赞 关注

回复
举报

68

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
MARK,

帮顶下。
 
 

回复

80

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
VB?過來,幫忙頂下》。。
 
 
 

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

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表