且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取给定ODBC驱动程序的ODBC驱动程序的DLL文件的名称

更新时间:2022-03-04 21:32:56

您可以将SQLGetInfo()InfoType=SQL_DRIVER_NAME

我希望它看起来像这样:

I hope this will look like:

Status := SQLGetInfo(ConnEnv, SQL_DRIVER_NAME, PAnsiChar(DriverName), 255, NameLen);

但是此功能适用于已连接的数据库.

But this function works with already connected database.

我尝试了SQLDrives(),而您是正确的:在我的环境中,该函数也不返回DLL名称.因此,我尝试从注册表中读取它,并以这种方式工作:

I tried SQLDrives() and you are right: in my environment this function also do not return DLL name. So I tried to read it from registry and it worked this way:

  DLLName := RegGetStringDirect(HKEY_LOCAL_MACHINE, 'SOFTWARE\ODBC\ODBCINST.INI\'  + DriverName, 'Driver');

对于驱动程序:IBM INFORMIX ODBC DRIVER我得到了:C:\informix\bin\iclit09b.dll

For driver: IBM INFORMIX ODBC DRIVER I got: C:\informix\bin\iclit09b.dll

对于驱动程序:SQL Server我得到了:C:\WINDOWS\system32\SQLSRV32.dll

For driver: SQL Server I got: C:\WINDOWS\system32\SQLSRV32.dll

RegGetStringDirect()是我基于Windows API的功能,用于从注册表中读取内容.

RegGetStringDirect() is my function based on Windows API to read something from registry.

由Ron Schuster读取"SQL Server" ODBC驱动程序dll名称的两个功能已从注释中移出:

Two functions to read "SQL Server" ODBC driver dll name by Ron Schuster moved from comment:

procedure TForm1.Button1Click(Sender: TObject); 
//using Windows API calls 
var 
  KeyName, ValueName, Value: string; 
  Key: HKEY; 
  ValueSize: Integer; 
begin
  ValueName := 'Driver';
  KeyName := 'SOFTWARE\ODBC\ODBCINST.INI\SQL Native Client';
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar(KeyName), 0, KEY_READ, Key) = 0 then
    if RegQueryValueEx(Key, PChar(ValueName), nil, nil, nil, @ValueSize) = 0 then begin
      SetLength(Value, ValueSize);
      RegQueryValueEx(Key, PChar(ValueName), nil, nil, PByte(Value), @ValueSize);
      ShowMessage(Value);
    end;
end; 

procedure TForm1.Button2Click(Sender: TObject);
//using TRegistry class 
var
  KeyName, ValueName, Value: string;
  Reg: TRegistry;
begin
  ValueName := 'Driver';
  KeyName := 'SOFTWARE\ODBC\ODBCINST.INI\SQL Native Client';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKeyReadOnly(KeyName) then begin
      Value := Reg.ReadString(ValueName);
      ShowMessage(Value);
    end;
  finally
    Reg.Free;
  end;
end;