当前位置: 源码素材网 » Delphi教程 » 详情页

Delphi 获取驱动列表并加载指定的驱动

  •   时间:2020-12-22
  • 概述:驱动程序

Delphi 获取驱动列表并加载指定的驱动,获取本机的驱动程序,不过程序在Windows 7下似乎有异常,有兴趣的Delphi 爱好者请自行修正一下代码哦,运行效果请参见下图:

Delphi 获取驱动列表并加载指定的驱动

具体的实现,请参见以下代码:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls,DBTables,DbiErrs, XPMan;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    GroupBox1: TGroupBox;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
procedure FindDriverList(var DriverList: TStringList);
procedure DBLoadDriver(DriverType: string);
implementation
{$R *.DFM}
procedure FindDriverList(var DriverList: TStringList);
var
  TmpCursor: hdbicur;
  Driver: DRVType;
  rslt: dbiResult;
begin
  Check(DbiOpenDriverList(TmpCursor));
 //调用FindDriverList获取驱动列表,TmpCursor是返回的游标句柄
  DriverList.Clear;
 //清空旧信息,添加新信息
  repeat
    rslt:= DbiGetNextRecord(TmpCursor, dbiNOLOCK, @Driver, nil);
 //调用FindDriverList从游标得到一项驱动的详细信息,保存到DRVTyp型结构中
    if (rslt <> DBIERR_EOF) then begin
      DriverList.Add(StrPas(Driver.szType))
    end;
  until rslt <> DBIERR_NONE;
  //枚举结束
  Check(DbiCloseCursor(TmpCursor));
  //关闭游标
end;
procedure DBLoadDriver(DriverType: string);
begin
  Check(DbiLoadDriver(PChar(DriverType)));
 //调用DBLoadDriver加载指定的驱动
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  DriverList:Tstringlist;
begin
  DriverList:=Tstringlist.Create;
  FindDriverList(DriverList);
  ListBox1.Items:=DriverList;
  //将驱动列表赋值给ListBox1
end;
//双击加载相应的驱动
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  try
    DbiLoadDriver(PChar(ListBox1.Items[ListBox1.ItemIndex]));
    showmessage('成功加载驱动:'+ListBox1.Items[ListBox1.ItemIndex]);
  except
    showmessage('加载驱动:'+ListBox1.Items[ListBox1.ItemIndex]+'失败!');
    exit;
  end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Check(DbiInit(nil));
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Check(DbiExit);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  Close;
end;
end.

    相关声明:

      若“Delphi 获取驱动列表并加载指定的驱动”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。