Installing a Custom ODBC Driver on Windows for SQL Anywhere
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
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