I am having trouble reading keys from the Registry using the RegOpenKey
API function in my MapBasic application.

I am trying to get a list of all of the ODBC data sources that are
listed in the directory.  My code runs but I am getting a blank string
returned instead of the data source name.

I have the put the exact same code into Visual Basic, made only the
essential changes, and it runs correctly.  The only difference of note
between the MapBasic Code and the VB6 code is that the some of the API
variables are dimensioned as Integer in MapBasic and Long in VB.   The
'RegOpenKey' function is executing correctly but the 'RegOpenKey' key is
executing but returning a blank string.

I have been using the registry API's for a long time and this is the
only one that I have had any trouble with.  Any suggestions?

Thanks,

Chris



The MapBasic program is....

Include "MAPBASIC.DEF"

Declare Sub Main
Declare Function RegOpenKey Lib "ADVAPI32.DLL" Alias "RegOpenKeyA"
(ByVal hKey As Integer, ByVal SubKey As String, pResult As Integer) As
Integer
Declare Function RegEnumKey Lib "ADVAPI32.DLL" Alias "RegEnumKeyA"
(ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As
String, ByVal cbName As Integer) As Integer
Declare Function RegCloseKey Lib "ADVAPI32.DLL" (ByVal hKey As Integer)
As Integer
 
Define HKEY_CURRENT_USER        -2147483647
Define HKEY_LOCAL_MACHINE -2147483646
     
Sub Main
  
  Dim iResult As Integer, hOdbcKey As Integer
  Dim sName As String, Buffer As String
  Dim cnt As Integer
  
  Print Chr$(12)
  iResult = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI",
hOdbcKey)
  cnt = 0
  If iResult = 0 Then
    Do
      Buffer = "                                "
      iResult = RegEnumKey(hOdbcKey, cnt, Buffer, len(Buffer))
      If iResult = 0 Then
        sName = LTrim$(RTrim$(Buffer))
        Print cnt & ", '" & sName & "'"
      End If
      cnt = cnt + 1
    Loop While iResult = 0
    iResult = RegCloseKey(HKEY_LOCAL_MACHINE)
  End If
   
End Sub


The Visual Basic Code is ... (to run, paste the code into a form with a
Listbox control on it called List1)

Option Explicit

Private Declare Function RegOpenKey Lib "ADVAPI32.DLL" Alias
"RegOpenKeyA" (ByVal hKey As Long, ByVal SubKey As String, pResult As
Long) As Long
Private Declare Function RegEnumKey Lib "ADVAPI32.DLL" Alias
"RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName
As String, ByVal cbName As Long) As Long
Private Declare Function RegCloseKey Lib "ADVAPI32.DLL" (ByVal hKey As
Long) As Long
 
Const HKEY_LOCAL_MACHINE = -2147483646

Private Sub Form_Load()
  
  Dim iResult As Long, hOdbcKey As Long
  Dim sName As String, Buffer As String
  Dim cnt As Integer
  
  iResult = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI",
hOdbcKey)
  cnt = 0
  If iResult = 0 Then
    Do
      Buffer = "                                "
      iResult = RegEnumKey(hOdbcKey, cnt, Buffer, Len(Buffer))
      If iResult = 0 Then
        sName = LTrim$(RTrim$(Buffer))
        List1.AddItem cnt & " - " & sName
      End If
      cnt = cnt + 1
    Loop While iResult = 0
    iResult = RegCloseKey(HKEY_LOCAL_MACHINE)
  End If
   
End Sub



CHRISTOPHER PRESCOTT
GIS Specialist 

TONKIN CONSULTING 
5 Cooke Terrace 
Wayville SA 5034 

[EMAIL PROTECTED] 



---------------------------------------------------------------------
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Message number: 3742

Reply via email to