Il codice di sotto modifica la chiave di registro ODBC.INI per leggere, tramite ODBC, un vecchio database.

Una routine andava bene per sistemi a 32bit (ps. windows 7 x32), mentre l'altra l'ho dovuta modificare per fare funzionare l'applicativo in un sistema a 64bit (ps windows 7 x64)

mi chiedevo se esistesse un modo per assiemare queste due routine, ossia fare in modo che il programma riconosca l'ambiente e scriva correttamente nel registro.

Ciao

codice:
Public StringaConnessione As String = "Dsn=ContoCorrentePietro;uid=pietro;pwd=***"

'-----------------------------------------------------------------------------------------------------------------------------
'vale per sistema a 32bit, es. windows 7 32bit
'-----------------------------------------------------------------------------------------------------------------------------
Public Sub CreaODBC_32bit()
    Dim DatabaseFile As String = System.Configuration.ConfigurationManager.AppSettings("DatabaseFile")
    Dim j As New Impersonation()
    Dim principalThreadUserName As String = WindowsIdentity.GetCurrent.Name
    If j.ImpersonateValidUser("Pietro", Nothing, "***") Then
        Dim impersonatingUserName As String = WindowsIdentity.GetCurrent.Name
        Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\ODBC\ODBC.INI\ContoCorrentePietro")
            If key Is Nothing Then 'se l'ODBC non è presente, lo creo
                Using keyNew As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\ODBC\ODBC.INI", True)
                    Using k As RegistryKey = keyNew.CreateSubKey("ContoCorrentePietro")
                        k.SetValue("Driver", "C:\Program Files\Sybase\SQL Anywhere 5.0\win32\wod50t.dll")
                        k.SetValue("Start", "C:\Program Files\Sybase\SQL Anywhere 5.0\win32\dbeng50.exe")
                        k.SetValue("DatabaseFile", DatabaseFile)
                        k.SetValue("DatabaseName", "Contocorrente")
                        k.SetValue("AutoStop", "yes")
                    End Using
                End Using
                Using SubKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", True)
                    If SubKey IsNot Nothing Then
                        SubKey.SetValue("ContoCorrentePietro", "Sybase SQL Anywhere 5.0")
                    End If
                End Using
            End If
        End Using

        j.UndoImpersonation()
    End If
End Sub


'-----------------------------------------------------------------------------------------------------------------------------
'vale per sistema a 64bit, es. windows 7 64bit
'-----------------------------------------------------------------------------------------------------------------------------
Public Sub CreaODBC()
    Dim ProgramFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
    Dim DatabaseFile As String = System.Configuration.ConfigurationManager.AppSettings("DatabaseFile")
    Dim j As New Impersonation()
    Dim principalThreadUserName As String = WindowsIdentity.GetCurrent.Name
    If j.ImpersonateValidUser("Pietro", Nothing, "***") Then
        Dim impersonatingUserName As String = WindowsIdentity.GetCurrent.Name
        Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\ODBC\ODBC.INI\ContoCorrentePietro")
            If key Is Nothing Then 'se l'ODBC non è presente, lo creo
                Using keyNew As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\ODBC\ODBC.INI", True)
                    Using k As RegistryKey = keyNew.CreateSubKey("ContoCorrentePietro")
                        k.SetValue("Driver", String.Format("{0}\Sybase\SQL Anywhere 5.0\win32\wod50t.dll", ProgramFilesX86))
                        k.SetValue("Start", String.Format("{0}\Sybase\SQL Anywhere 5.0\win32\dbeng50.exe", ProgramFilesX86))
                        k.SetValue("DatabaseFile", DatabaseFile)
                        k.SetValue("DatabaseName", "Contocorrente")
                        k.SetValue("AutoStop", "yes")
                    End Using
                End Using
                Using SubKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources", True)
                    If SubKey IsNot Nothing Then
                        SubKey.SetValue("ContoCorrentePietro", "Sybase SQL Anywhere 5.0")
                    End If
                End Using
            End If
        End Using

        j.UndoImpersonation()
    End If
End Sub