views:

395

answers:

2

I have an application that connects via a DSN to an Oracle database. If the initial attempt to connect fails, then I make sure their DSN exists. If it does not exist, then I create it using the SQLConfigDataSource command.

That command requires the driver name as one of its arguments. On my machine, I have the 11g driver, so the following works:

const
  cDriver = 'Oracle in OraDb11g_home1' + #0;
var
  strAttr: string;
begin
  strAttr := 'DSN=' + DSNName + #0 +
             'SERVER=' + TNSName + #0;
  SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,PChar(cDriver),PChar(strAttr));
end;

But the client machine might have a different version of Oracle, or a different name for their oracle home. How can I tell which driver to use on an arbitrary machine?

I'm using Delphi, but it shouldn't matter much, since this is just an API call anyway.

+1  A: 

You might try delving into the SQLDrivers call to see about getting the installed drivers on the system you're on.

DCookie
+1  A: 

I ended up using the list in the registry, as shown here:

function TDSNManager.GetOracleDriverName: string;
var
  reg : TRegistry;
  drivers: TStringList;
  i: integer;
begin
  drivers := TStringList.Create;
  reg := nil;
  try
    reg := TRegistry.Create;
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI',False) then begin
      reg.GetKeyNames(drivers);
    end;
  finally
    FreeAndNil(reg);
  end;  //try-finally

  for i := 0 to drivers.Count - 1 do begin
    if 0 < Pos('ORACLE IN',Uppercase(drivers[i])) then begin
      Result := drivers[i];
      Break;
    end;
  end;
end;
JosephStyons