Com esta dica você poderá criar drivers ODBC em tempo de execução, softwares de instalação, economizando tempo e deixando suas aplicações mais dinâmicas.


unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
 
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
 
var
Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
procedure CreateODBCDriver(Const 
cDSNName,cExclusive,cDescription,cDataBase,cDefaultPath,cConfigSql,cDriver: string);
 
type
 
TSQLConfigDataSource = function( hwndParent: HWND; fRequest: WORD; lpszDriver: LPCSTR; 
lpszAttributes: LPCSTR ): BOOL; stdcall;
 
const
  ODBC_ADD_DSN = 1; // Adiciona uma fonte de dados (data source)
  ODBC_CONFIG_DSN = 2; // Configura a fonte de dados (data source)
  ODBC_REMOVE_DSN = 3; // Remove a fonte de dados (data source)
  ODBC_ADD_SYS_DSN = 4; // Adiciona um DSN no sistema
  ODBC_CONFIG_SYS_DSN = 5; // Configura o DSN do sistema
  ODBC_REMOVE_SYS_DSN = 6; // Remove o DSN do sistema
var
  pFn: TSQLConfigDataSource;
  hLib: LongWord;
  strDriver: string;
  strHome: string;
  strAttr: string;
  strFile: string;
  fResult: BOOL;
  ModName: array[0..MAX_PATH] of Char;
  srInfo : TSearchRec;
begin
Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
strHome := ModName;
while ( strHome[length(strHome)] <> '\' ) do
Delete( strHome, length(strHome), 1 );
strFile := strHome + cDatabase; // Teste com access (Axes = Access)
hLib := LoadLibrary( pChar(cDefaultPath) ); // carregando para o diretório padrão
if( hLib <> NULL ) then 
begin
@pFn := GetProcAddress( hLib, pChar(cConfigSql) );
if( @pFn <> nil ) then 
begin 
strDriver := cDriver;
strAttr := Format( 'DSN=%s'+#0+
'DBQ=%s'+#0+
'Exclusive=%s'+#0+
'Description=%s'+#0+#0,
[cDSNName,strFile,cExclusive,cDescription] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then
ShowMessage( 'Falha ao tentar criar o DSN (Data source).' );
 
if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
begin
  strDriver := cDriver;
 
strAttr := Format( 'DSN=%s'+#0+
'DBQ=%s'+#0+
'Exclusive=%s'+#0+
'Description= %s'+#0+#0+
'CREATE_DB="%s"'#0+#0,
[cDSNName,strFile,cExclusive,cDescription,strFile]);
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then
  ShowMessage( 'Falha ao tentar criar o banco de dados' );
end;
FindClose( srInfo );
 
end;
FreeLibrary( hLib );
if fResult then
  ShowMessage( 'Banco de dados criado.' );
end
else
begin
  ShowMessage( 'o sistema não pode carregar a biblioteca ODBCCP32.DLL' );
end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateOdbcDriver('Cludelphi DSN','1','clubedelphi','clubedelphi.MDB','ODBCCP32','SQLConfigDataSource',
  'Microsoft Access 
  Driver (*.mdb)');
end;
 
end