Accueil > > > LIRE ET ÉCRIRE DANS LA BASE DE REGISTRE FACILEMENT QUELQUESOIT L'UTILISATEUR
LIRE ET ÉCRIRE DANS LA BASE DE REGISTRE FACILEMENT QUELQUESOIT L'UTILISATEUR
Information sur la source
Description
Tout le monde connait la fonction GetSettings de VB6. Elle pose un petit pb sous W2K : elle écrit sous une clé qui dépends de l'utilisateur. Si vous voulez que vos données soient communes à tous les utilisateurs, il faut écrire sous LOCAL_MACHINE et non USER. Bref, voici une focntion GetSettingsEX() avec les même paramètre que GetSettings originale. Remarque : si la clé n'existe pas, elle est créée automatiquement...
Source
- Option Explicit
-
- Private Const REG_SZ = 1 ' Unicode nul terminated string
- Private Const REG_BINARY = 3 ' Free form binary
- Private Const HKEY_LOCAL_MACHINE = &H80000002
-
- Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
- Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
- Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
- 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.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
- Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
-
- Public Function GetSettigsEX(appName As String, section As String, key As String, Optional defValue As String)
- Dim strpath As String, res As String
-
- strpath = "SOFTWARE\" + appName + "\" + section
- res = GetString(HKEY_LOCAL_MACHINE, strpath, key)
-
- If res = "" And Not IsMissing(defValue) Then
- SaveString HKEY_LOCAL_MACHINE, strpath, key, defValue
- res = defValue
- End If
-
- GetSettigsEX = res
-
- End Function
-
- Private Function GetString(hKey As Long, strpath As String, strValue As String) As String
- Dim Ret As Long
- 'Open the key
- RegOpenKey hKey, strpath, Ret
- 'Get the key's content
- GetString = RegQueryStringValue(Ret, strValue)
- 'Close the key
- RegCloseKey Ret
- End Function
-
- Private Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
- Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
- 'retrieve nformation about the key
- lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
- If lResult = 0 Then
- If lValueType = REG_SZ Then
- 'Create a buffer
- strBuf = String(lDataBufSize, Chr$(0))
- 'retrieve the key's content
- lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
- If lResult = 0 Then
- 'Remove the unnecessary chr$(0)'s
- RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
- End If
- ElseIf lValueType = REG_BINARY Then
- Dim strData As Integer
- 'retrieve the key's value
- lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
- If lResult = 0 Then
- RegQueryStringValue = strData
- End If
- End If
- End If
- End Function
-
- Private Sub SaveString(hKey As Long, strpath As String, strValue As String, strData As String)
- Dim Ret
- 'Create a new key
- RegCreateKey hKey, strpath, Ret
- 'Save a string to the key
- RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
- 'close the key
- RegCloseKey Ret
- End Sub
-
- Private Sub SaveStringLong(hKey As Long, strpath As String, strValue As String, strData As String)
- Dim Ret
- 'Create a new key
- RegCreateKey hKey, strpath, Ret
- 'Set the key's value
- RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
- 'close the key
- RegCloseKey Ret
- End Sub
-
- Private Sub DelSetting(hKey As Long, strpath As String, strValue As String)
- Dim Ret
- 'Create a new key
- RegCreateKey hKey, strpath, Ret
- 'Delete the key's value
- RegDeleteValue Ret, strValue
- 'close the key
- RegCloseKey Ret
- End Sub
-
Option Explicit
Private Const REG_SZ = 1 ' Unicode nul terminated string
Private Const REG_BINARY = 3 ' Free form binary
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
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.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Function GetSettigsEX(appName As String, section As String, key As String, Optional defValue As String)
Dim strpath As String, res As String
strpath = "SOFTWARE\" + appName + "\" + section
res = GetString(HKEY_LOCAL_MACHINE, strpath, key)
If res = "" And Not IsMissing(defValue) Then
SaveString HKEY_LOCAL_MACHINE, strpath, key, defValue
res = defValue
End If
GetSettigsEX = res
End Function
Private Function GetString(hKey As Long, strpath As String, strValue As String) As String
Dim Ret As Long
'Open the key
RegOpenKey hKey, strpath, Ret
'Get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Private Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Private Sub SaveString(hKey As Long, strpath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strpath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey Ret
End Sub
Private Sub SaveStringLong(hKey As Long, strpath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strpath, Ret
'Set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Private Sub DelSetting(hKey As Long, strpath As String, strValue As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strpath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
Conclusion
Mettre ce code dans un module(.bas), et ajouter ce module à votre projet...
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS !PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS ! par Etienne Margraff
J'ai récemment eu un problème pour obtenir l'intelliTrace sur un site web dans IIS. Il n'y avait pas de message d'erreur, rien dans le journal d'évènement Windows, et après 3 appels à une voyante, 2 visites chez un marabou, j'ai failli me résign...
Cliquez pour lire la suite de l'article par Etienne Margraff OFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONSOFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONS par junarnoalg
De nombreuses entreprises font le choix de SharePoint Online, service fourni au travers de l'offre de Microsoft Office 365. S'il est vrai que ce choix apporte un grand nombre d'avantages; rapidité de mise en œuvre, disponibilité, large couvertu...
Cliquez pour lire la suite de l'article par junarnoalg PRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGEPRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGE par richardc
http://www.c2idotnet.com/articles/presentation-des-api-rest-de-windows-azure-lister-les-comptes-de-storage
Désolé pour "toto", mais c2i existait avant blogs.developpeur.org et c'est mon site "officiel" ;-) ...
Cliquez pour lire la suite de l'article par richardc [HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Très bonne après-midi passée lors cette conférence avec le W3C, organisée par L' Inria sur les nouveaux standards, ce Mardi 14 Février, on sent vraiment que çà bosse au W3C, et l'avenir est très très prometteur pour le HTML5, notamment ...
Cliquez pour lire la suite de l'article par Gio
Forum
FONCTION EXCEL VBAFONCTION EXCEL VBA par samanta26
Cliquez pour lire la suite par samanta26
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|