I'm trying to create a new system DSN entry when my service is installed. I tried to do this by writing to the registry via TRegistry.OpenKey and TRegistry.WriteString, but no values are being written. The first of the three keys that I'm writing to works fine.
const
sODBCRegKeyLoc = '\SOFTWARE\ODBC\ODBC.INI\OBDC Data Sources';
sServerRegKeyLoc = '\SOFTWARE\ODBC\ODBC.INI\DSN Name';
rInstall := TRegistry.Create(KEY_READ or KEY_WRITE);
try
rInstall.RootKey := HKEY_LOCAL_MACHINE;
if rInstall.OpenKey(sRegKeyLoc, True)
then
begin
rInstall.WriteString('Description', 'Monitors for new log entries. Allows modification');
rInstall.CloseKey;
end; //This call works fine.
if rInstall.OpenKey(sODBCRegKeyLoc, True)
then
begin
rInstall.WriteString('DSN Name', 'SQL Native Client');
end; //This call fails with no error message.
if rInstall.OpenKey(sServerRegKeyLoc, True)
then
begin
rInstall.WriteString('Driver','c:\Windows\system32\sqlncli.dll');
rInstall.WriteString('Server','serverIP\SQLEXPRESS');
rInstall.WriteString('Database', 'Databasename');
end; //This call fails with no error message.
finally
rInstall.Free;
end; //Write values to registry.
Any help would be appreciated.
The following possible failure modes come to mind:
HKLM
therefore fail but your code fails to report that error. HKLM\Software\Wow6432Node
. You'll need an application manifest to avoid virtualization. You need to use the requireAdministrator
setting for requestedExecutionLevel
.
Use KEY_WOW64_64KEY
to specify access to the 64 bit view from your 32 bit program.