• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Reading Registry in VB6

I would like to see if an ODBC DSN has been created from my VB program. So I need to read the registry to see if it has been created?  Any help is greatly appreciated.
0
shubbell
Asked:
shubbell
  • 7
  • 5
  • 3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
I commonly recreate ODBC DSN every time I start an application.

It is so quick to recreate that I don't loose time checking for existence!

To recreate it, I use the RegisterDatabase method (of DAO) or rdoRegisterDatabase (of RDO).
0
 
shubbellAuthor Commented:
emoreau, maybe I'm not clear.  I want to check to see if a particular OBDC DSN entry has been created from inside my VB program.  If it has not, then i must tell the user to quit my VB program and go create it and then start the VB program up again.  
0
 
Éric MoreauSenior .Net ConsultantCommented:
Why ask your user to create a DSN when your application can create one (with RegisterDatabase)?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
shubbellAuthor Commented:
emoreau, I have no idea. Seems stupid to me too. But don't you think I need to check to see if it exists before I write over it with RegisterDatabase?
0
 
Éric MoreauSenior .Net ConsultantCommented:
As I already said, it is so fast registering it (less than a second) that I don't check if it already exist even if I will overwrite it with the same value.

By overwriting it, I am sure that my DSN is always as I want it to be!
0
 
shubbellAuthor Commented:
emoreau, I really need to check it before writing it with RegisterDatabase.  I do not trust my users to name it the same thing everytime and other programs depend on this particular OBDC DSN.  Do you know of anyway to check it first? Thank you.
0
 
manojaminCommented:
modified for your need, got it from

HOWTO: Retrieve Printer Name from Windows 95/98 Registry in VB
http://support.microsoft.com/support/kb/articles/Q143/2/74.asp

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias _
    "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
    phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32" Alias _
      "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName$, ByVal _
      lpdwReserved As Long, lpdwType As Long, lpData As Any, lpcbData As _
      Long) As Long

Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As _
      Long) As Long


Private Const HKEY_LOCAL_MACHINE = &H80000002

    Function IsExistKey(hInKey As Long, ByVal subkey$, ByVal valname$) As Boolean
        Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long
        Dim R As Long

        RetVal$ = ""
        Const KEY_ALL_ACCESS As Long = &HF0063
        Const ERROR_SUCCESS As Long = 0
        Const REG_SZ As Long = 1

        R = RegOpenKey(hInKey, subkey$, hSubKey)
'        If R <> ERROR_SUCCESS Then GoTo Quit_Now
        If R <> ERROR_SUCCESS Then Exit Function

        SZ = 256: v$ = String$(SZ, 0)
        R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
        If R = ERROR_SUCCESS And dwType = REG_SZ Then
            IsExistKey = True
            'RetVal$ = Left$(v$, SZ)
'        Else
            'RetVal$ = "--Not String--"
        End If
        If hInKey = 0 Then R = RegCloseKey(hSubKey)
'Quit_Now:
'        IsExistKey$ = RetVal$
    End Function
 

Private Sub Form_Load()
          ifdsn = IsExistKey(HKEY_LOCAL_MACHINE, _
      "SOFTWARE\ODBC\ODBC.INI\YourDsnName", "Driver")
      MsgBox ifdsn
End Sub

0
 
shubbellAuthor Commented:
manojamin, that is more of what I was looking for, however, i can not get it to give me the correct answer. In Function IsExistKey "R" always has a value of 2 whether the DSN exists or not.  Any clues?  Thank you so much.
0
 
manojaminCommented:
after which call?

R = RegOpenKey(hInKey, subkey$, hSubKey)

or

R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)


0
 
shubbellAuthor Commented:
manojamin, after
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ).  It always returns 2 which after formating the message says "Unable to find file specified".
0
 
shubbellAuthor Commented:
manojamin, please disregard my stupidity. It is after
R = RegOpenKey(hInKey, subkey$, hSubKey) that i get the value of 2.  THanks.
0
 
manojaminCommented:
'forget about that, if it does not work, use this

Private Const SQL_FETCH_NEXT As Long = 1
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
Private Declare Function SQLDataSources Lib "odbc32.dll" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
Sub GetDSNsAndDrivers()
    On Error Resume Next
   
    Dim i As Integer
    Dim sDSNItem As String * 1023
    Dim sDRVItem As String * 1023
    Dim sDSN As String
    Dim sDRV As String
    Dim iDSNLen As Integer
    Dim iDRVLen As Integer
    Dim lHenv As Long         'handle to the environment


    'get the DSNs
    If SQLAllocEnv(lHenv) <> -1 Then
        Do Until i <> SQL_SUCCESS
            sDSNItem = Space(1024)
            sDRVItem = Space(1024)
            i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
            sDSN = VBA.Left(sDSNItem, iDSNLen)
            sDRV = VBA.Left(sDRVItem, iDRVLen)
            Debug.Print sDSN
        Loop
    End If
End Sub

'this will print all the system dsn in debug window...

0
 
manojaminCommented:
make this as

    Dim sDSNItem As String * 1023
    Dim sDRVItem As String * 1023

not

    Dim sDSNItem As String * 1024
    Dim sDRVItem As String * 1024

does not really make difference though...
0
 
manojaminCommented:
Oh dear God! another mistake!!!!
make this as

    Dim sDSNItem As String * 1024
    Dim sDRVItem As String * 1024

not

    Dim sDSNItem As String * 1023
    Dim sDRVItem As String * 1023

does not really make difference though
0
 
shubbellAuthor Commented:
thank you
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now