Search code examples
delphiodbcparadox

What values do I pass to the Attributes parameter of SQLConfigDataSource to create a system DSN for the Microsoft Paradox Driver


I am trying to add a system DSN for the Microsoft Paradox Driver (ODBC) programmatically, and I cannot find any documentation on the keys that I need to pass in the attributes parameter of SQLConfigDataSource. I can successfully add an MS Access system DSN, but that is because there are a number of examples out there that include the keys (DBQ, for example). My code (Delphi), which does not work, is shown below.

I have tried a large number of different keys, but I have not been successful. For example, I inspected the name/value pairs that appear under HKEY_LOCAL_MACHINE\Software\Wow6432Node\ODBC\ODBC.INI (32-bit ODBC) in the registry, but that didn't lead to a solution.

Does anyone know what keys I need to pass in the lpszAttributes parameter of SQLConfigDataSource to create a Paradox system DSN programmatically?

function SQLConfigDataSource (
    hwndParent:     SQLHWnd;
    fRequest:       WORD;
    lpszDriver:     PChar;
    lpszAttributes: PChar
  ): SQLBOOL; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl {$ENDIF};
  external 'odbccp32.dll' name 'SQLConfigDataSourceW';

procedure TForm1.Button1Click(Sender: TObject);
var
  Attributes: string;
  RetVal: Boolean;
begin
  Attributes := 'DSN=' + 'Paradox Data#0;
  Attributes := Attributes + 'DESCRIPTION=Paradox DSN for sample data'#0;
  Attributes := Attributes + 'DEFAULTDIR=c:\Users\Public\Documents\RAD Studio\12.0\Samples\Data'#0#0;
  RetVal := SqlConfigDataSource(0, ODBC_ADD_SYS_DSN, 'Microsoft Paradox Driver (*.db)', PChar(Attributes));
  if not RetVal then
    ShowMessage('Could not add DSN');
end;

I originally reported the answer here, but both warrenp and crefird suggested that I answer my own question (even though credit goes to crefird). You will find my answer below.


Solution

  • The solution has been found. crefird posted a link to the Paradox ODBC driver connection string in the first comment to this question, and using the names found there I was able to create the ODBC system DSN (data source name).

    I was close on my initial try, but you won't believe what was missing. I didn't have the driver name exactly correct. In my above code I entered the driver name as this

    'Microsoft Paradox Driver (*.db)'
    

    The correct driver name is this

    'Microsoft Paradox Driver (*.db )'
    

    Yes, that extra space before the close paren is actually the correct driver name. Wow!

    Here are two routines that I ended up writing to dynamically create a DSN:

    implementation
    
    uses Registry,  Winapi.Windows, System.SysUtils;
    
    const
      ODBC_ADD_SYS_DSN    = 4;  // add a system DSN
    
    function SQLConfigDataSource( hwndParent: LongWord ; fRequest: Word ;
      lpszDriver: PChar ; lpszAttributes: pchar ): boolean;
      stdcall; external 'ODBCCP32.DLL' name 'SQLConfigDataSourceW';
    
    procedure CreateParadoxDSN(DataSourceName: string; DataDirectory: string);
    var
      Attributes: string;
      RetVal: Boolean;
      DriverName: PChar;
      DirName: string;
    begin
      DriverName := 'Microsoft Paradox Driver (*.db )';
      Attributes := 'DSN=' + DataSourceName + #0;
      Attributes := Attributes + 'DefaultDir=' + DataDirectory + #0;
      Attributes := Attributes + 'Dbq=' + DataDirectory + #0;
      Attributes := Attributes + 'UID='#0;
      Attributes := Attributes + 'Fil=Paradox 5.0'#0#0;
      Attributes := Attributes + 'DESCRIPTION=' + DataSourceName + #0#0;
      RetVal := SqlConfigDataSource(0, ODBC_ADD_SYS_DSN, DriverName,
                                    PChar(Attributes));
      if not RetVal then
      begin
        Exception.Create('Failed to create data source name. Cannot continue');
      end;
    end;
    
    function ParadoxDSNExists(DataSourceName: string): Boolean;
    var
      Registry: TRegistry;
    begin
      Registry := TRegistry.Create;
      try
        Registry.RootKey := HKEY_LOCAL_MACHINE;
        Result := Registry.KeyExists('Software\Wow6432Node\ODBC\ODBC.INI\' +
                                  DataSourceName);
      finally
        Registry.Free;
      end;
    end;