dcsimg
December 10, 2016
Hot Topics:

Installing a Custom ODBC Driver on Windows for SQL Anywhere

  • November 21, 2007
  • By Jason Hinsperger
  • Send Email »
  • More Articles »

Automatically Creating the ODBC Driver Entry

The Windows API provides methods for accessing and modifying the Registry to add the new ODBC driver as part of an application installation. The following function demonstrates how a new Registry key can be created and set for your ODBC driver programmatically.

STDAPI RegisterCustomODBCDriverName( char * DriverName,
                                     char * InstallPath )
{
   char     key[1024];
   char     basekey[1024];
   int           path_len;
   HKEY          hKey;
   int           ret;

   strcpy( basekey, "SOFTWARE\\ODBC\\ODBCINST.INI\\" );

   // Make Registry entries for the ODBC driver.
   // odbc\odbcinst.ini\odbc drivers\DriverName=installed
   strcpy( key, basekey );
   strcat( key, "ODBC Drivers" );
   ret = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
                         key,
                         0,
                         NULL,
                         REG_OPTION_NON_VOLATILE,
                         KEY_ALL_ACCESS,
                         NULL,
                         &hKey,
                         NULL   );

   ret = RegSetValueEx( hKey,
                        DriverName,
                        0,
                        REG_SZ,
                        (unsigned char *)"Installed",
                        10 );    // length of 'Installed' + NULLCHAR

   // odbc\odbcinst.ini\DriverName\driver=<path>\dbodbc?.dll
   // odbc\odbcinst.ini\DriverName\setup=<path>\dbodbc?.dll
   strcpy( key, basekey );
   strcat( key, DriverName );
   ret = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
                         key,
                         0,
                         NULL,
                         REG_OPTION_NON_VOLATILE,
                         KEY_ALL_ACCESS,
                         NULL,
                         &hKey,
                         NULL   );

   path_len = _strlen( InstallPath ) + 1;    // include NULLCHAR
   ret = RegSetValueEx( hKey,
                        "Driver",
                        0,
                        REG_SZ,
                        (unsigned char *)InstallPath,
                        path_len );

   ret = RegSetValueEx( hKey,
                        "Setup",
                        0,
                        REG_SZ,
                        (unsigned char *)InstallPath,
                        path_len );

   return S_OK;
}

Now that you have created the new driver, you also must be able to remove it as part of a good uninstall process. The following function will remove the driver entries from the Registry:

UnRegisterCustomODBCDriverName( char * DriverName )
{
   char     key[1024];
   char     basekey[1024];
   HKEY          hKey;

   //Construct the Registry key to open
   strcpy( basekey, "SOFTWARE\\ODBC\\ODBCINST.INI\\" );
   strcpy( key, basekey );
   strcat( key, "ODBC Drivers" );

   //Open the Registry key
   if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                     key,
                     0,
                     KEY_ALL_ACCESS,
                     &hKey      ) != ERROR_SUCCESS ) {

      return E_UNEXPECTED;
   }

   strcpy( key, DriverName );

   //Remove the driver if it already exists
   if( RegDeleteValue( hKey,
                       key      ) {

      return E_UNEXPECTED;
   }

   strcpy( key, basekey );
   strcat( key, DriverName );

   if( RegDeleteKey( HKEY_LOCAL_MACHINE,
                     key     ) != ERROR_SUCCESS ) {

      return E_UNEXPECTED;
   }

   return S_OK;
}

Final Remarks

By creating your own custom ODBC driver entry, you effectively can isolate your application database connectivity components from other applications running on the same system. This decreases the chance for deployment problems, and problems related to applying maintenance or other upgrades. In turn, this decreases the amount of installation and configuration support that will be required during deployment, and improves the overall end user experience with your application.

About the Author

Jason Hinsperger has been working with SQL Anywhere for over 13 years. In his current role as a Senior Product Manager with Sybase iAnywhere, Hinsperger regularly shares his in-depth knowledge about SQL Anywhere through articles and talks. When he's not working with SQL Anywhere or learning new technologies, Hinsperger spends his time outdoors running a cattle farm in southern Ontario, Canada.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel