Bonjour,

J'essaie de récupérer toutes les valeurs (Sources de données ODBC) sous la clé de registre... mais là, j'appelle au secours !

Dans le code suivant, la fonctionRegOpenKeyEx échoue toujours (avec par conséquent obtention du message "Non trouvé").

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
Public Class Form1
 
  Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByVal phkResult As Long) As Long
  Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
    Dim str_ExcelFilePathAndName As String
 
 
    OpenFileDialog1.ShowDialog()
    str_ExcelFilePathAndName = OpenFileDialog1.FileName
 
    MsgBox(str_ExcelFilePathAndName)
 
    Const cst_HKEY_LOCAL_MACHINE = &H80000002
    Const cst_KEY_READ = &H20019 '((READ_CONTROL Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const str_KeyName As String = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
 
    EnumRegistryKeys(cst_HKEY_LOCAL_MACHINE, str_KeyName, cst_KEY_READ)
 
  End Sub
 
Function EnumRegistryKeys(ByVal lng_hKey As Long, ByVal str_KeyName As String, ByVal lng_hKeyRead As Long) As Collection
' Enumerate registry keys under a given key and returns a collection of strings
 
  Dim hdl_Handle As Long
  Dim lng_Length As Long
  Dim lng_Index As Long
  Dim str_SubKeyName As String
 
  Const lng_KeyNameLength = 260
 
  ' initialize the result collection
  EnumRegistryKeys = New Collection
 
  ' Open the key, exit if not found
  If Len(str_KeyName) Then
 
    If RegOpenKeyEx(lng_hKey, str_KeyName, 0, lng_hKeyRead, hdl_Handle) Then
      MsgBox("Non trouvé")
      Exit Function
    End If
    '   in all case the subsequent functions use hKey
    lng_hKey = hdl_Handle
 
  End If
 
  Do
    ' this is the max length for a key name
 
    str_SubKeyName = Space$(lng_Length)
    ' get the N-th key, exit the loop if not found
 
    If RegEnumKey(lng_hKey, lng_Index, str_SubKeyName, lng_KeyNameLength) Then
      Exit Do
    End If
 
    ' add to the result collection
    str_SubKeyName = Mid(str_SubKeyName, 1, InStr(str_SubKeyName, vbNullChar) - 1)
    EnumRegistryKeys.Add(str_SubKeyName, str_SubKeyName)
    ' prepare to query for next key
    lng_Index = lng_Index + 1
 
  Loop
 
  ' Close the key, if it was actually opened
  If hdl_Handle Then
    RegCloseKey(hdl_Handle)
  End If
 
End Function
 
End Class
Quelqu'un peut-il me mettre sur la voie ?

Merci infiniment !
Drooxy