Question

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.

Was it helpful?

Solution

The following possible failure modes come to mind:

  1. Your process has no manifest and so is virtualized. The writes succeed but the data lands in the virtual store.
  2. Your process has a manifest but does not run elevated. The process is not virtualized. The attempts to open keys for writing under HKLM therefore fail but your code fails to report that error.
  3. The process is 32 bit and so the access is redirected to the 32 bit view of the registry. The writes succeed but the data lands in the 32 view under 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top