How to get name of ODBC driver's DLL file for a given ODBC driver

微笑、不失礼 提交于 2019-12-24 08:57:14

问题


How do I programatically get the name of an ODBC driver's DLL file for a given ODBC driver. For example, given "SQL Server Native Client 10.0" I want to find the name of that driver's DLL file: sqlncli10.dll. I can see this in REGEDIT in the "Driver" entry in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI. If I try to read the value from the registry in my code it returns an empty string. I also tried using the ODBC API function SQLDrivers. The code below successfully returns all the values of the attributes in the Attribs variable except "Driver". Everything is there - APILevel, ConnectFunctions, CPTimeout, etc - but "Driver" is not in the list.

repeat
  Status := SQLDrivers (HENV, SQL_FETCH_NEXT, PAnsiChar(DriverName), 255,
            NameLen, PAnsiChar(Attribs), 1024, AttrLen);
  if Status = 0 then begin
    List.Add(DriverName);
    List.Add(Attribs);
  end;
until Status <> 0;

回答1:


You can use SQLGetInfo() with 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.

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');

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

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

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

EDIT:

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;


来源:https://stackoverflow.com/questions/13956198/how-to-get-name-of-odbc-drivers-dll-file-for-a-given-odbc-driver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!