Unit em Delphi para Consultas de DSNs
Autor: Jullian H Moreira | Publicado: 2011-09-23T19:58:00-07:00


unit DSNFuncs;

{Créditos para &nbsp;Oscar Noe Martin

Fonte pego no endereço http://www.delphi3000.com/articles/article_3584.asp?SK=
Comentários traduzidos por Jullian Henrique Moreira
23/09/2011
}

interface
Uses Windows, Classes, SysUtils, Registry;

Type
&nbsp; TDSN_Type &nbsp; &nbsp; &nbsp; = (dtUSER, dtSYSTEM);
&nbsp; TDSN_Types &nbsp; &nbsp; &nbsp;= Set of TDSN_Type;


function GetODBCDriversList: TStrings;
function GetDSNList (aDSN_TYPES: TDSN_Types; aDriverFilter: String): TStrings;

implementation

function GetODBCDriversList: TStrings;

&nbsp; //Esta função retornará uma lista dos Drivers ODBC instalados
&nbsp; // Example: combobox1.Assign(GetODBCDriversList); // ;)

var
&nbsp; aStringlist &nbsp; : TStringlist;
&nbsp; aRegistry &nbsp; : TRegistry;
Begin
&nbsp; aStringlist:= Tstringlist.Create;
&nbsp; aRegistry:= TRegistry.Create;
&nbsp; Result:= Tstringlist.Create;

&nbsp; with aRegistry do
&nbsp; Begin
&nbsp; &nbsp; rootkey:= HKEY_LOCAL_MACHINE;
&nbsp; &nbsp; OpenKey('Software\ODBC\ODBCINST.INI\ODBC Drivers',False);
&nbsp; &nbsp; GetValueNames(aStringlist);
&nbsp; End;
&nbsp; aRegistry.Free;
&nbsp; aStringlist.Sort;
&nbsp; result.AddStrings(aStringlist);
&nbsp; aStringlist.Free;
End;

function GetDSNList (aDSN_TYPES: TDSN_Types; aDriverFilter: String): TStrings;

&nbsp; // Esta função retorna uma lista de DSNs( DSNs de Usuários (dtUser),
&nbsp; // DSNs de Sistema (dtSystem), ou ambas), onde o Driver contém aDriverFilter,
&nbsp; // pode ser o nome completo do driver ou apenas parte dele;
&nbsp; // &nbsp; &nbsp; &nbsp; (Exemplo: 'Microsoft Access Driver (*.mdb)' or 'Access').
&nbsp; //
&nbsp; // Exemplo: listbox1.Items.Assign(GetDSNList([dtUser,dtSystem],'Access');
&nbsp; // Irá retornar uma lista com todos os DSNs existentes tanto User quanto System
&nbsp; // com a string 'Access' no nome do Driver.

var
&nbsp; aStringList : TStringlist;

&nbsp; aStrings1 &nbsp; : TStrings;
&nbsp; aStrings2 &nbsp; : Tstrings;
&nbsp; aString &nbsp; &nbsp; : String;

&nbsp; aRegistry &nbsp; : TRegistry;
&nbsp; aInt &nbsp; &nbsp; &nbsp; &nbsp;: Integer;
Begin
&nbsp; aStringlist:=TStringlist.Create;
&nbsp; aStrings1:=TStringlist.Create;
&nbsp; aStrings2:=TStringlist.Create;

&nbsp; If dtUSER in aDSN_TYPES then
&nbsp; Begin
&nbsp; &nbsp; aRegistry:= Tregistry.Create;
&nbsp; &nbsp; With aRegistry do
&nbsp; &nbsp; Begin
&nbsp; &nbsp; &nbsp; RootKey:=HKEY_CURRENT_USER;
&nbsp; &nbsp; &nbsp; OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',False);
&nbsp; &nbsp; &nbsp; GetValueNames(aStrings1);
&nbsp; &nbsp; &nbsp; for aInt:=aStrings1.Count-1 downto 0 do
&nbsp; &nbsp; &nbsp; Begin
&nbsp; &nbsp; &nbsp; &nbsp; aString:= ReadString(aStrings1.Strings[aInt]);
&nbsp; &nbsp; &nbsp; &nbsp; if ((Pos( aDriverFilter, aString ) = 0) and (aDriverFilter&lt;&gt;'')) &nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aStrings1.Delete(aInt);
&nbsp; &nbsp; &nbsp; End;
&nbsp; &nbsp; End;
&nbsp; &nbsp; aRegistry.Free;
&nbsp; end;

&nbsp; If dtSYSTEM in aDSN_TYPES then
&nbsp; Begin
&nbsp; &nbsp; aRegistry:= Tregistry.Create;
&nbsp; &nbsp; With aRegistry do
&nbsp; &nbsp; Begin
&nbsp; &nbsp; &nbsp; RootKey:=HKEY_LOCAL_MACHINE;
&nbsp; &nbsp; &nbsp; OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',False);
&nbsp; &nbsp; &nbsp; GetValueNames(aStrings2);
&nbsp; &nbsp; &nbsp; for aInt:=aStrings2.Count-1 downto 0 do
&nbsp; &nbsp; &nbsp; Begin
&nbsp; &nbsp; &nbsp; &nbsp; aString:= ReadString(aStrings2.Strings[aInt]);
&nbsp; &nbsp; &nbsp; &nbsp; if ((Pos( aDriverFilter, aString) = 0) and (aDriverFilter&lt;&gt;'')) &nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aStrings2.Delete(aInt);
&nbsp; &nbsp; &nbsp; End;
&nbsp; &nbsp; End;
&nbsp; &nbsp; aRegistry.Free;
&nbsp; end;

&nbsp; aStringlist.AddStrings(aStrings1);
&nbsp; aStrings1.Free;
&nbsp; aStringlist.AddStrings(aStrings2);
&nbsp; aStrings2.Free;
&nbsp; aStringlist.Sort;
&nbsp; result:= Tstringlist.Create;
&nbsp; result.Assign(aStringlist);
End;

end.