Par APIs, ca donne.
Private
Declare
Function
RegOpenKey
Lib
"advapi32.dll"
Alias
"RegOpenKeyA"
(
ByVal
hKey
As
Long
,
ByVal
lpSubKey
As
String
,
ByVal
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
,
ByVal
lpType
As
Long
,
ByVal
lpData
As
String
,
ByVal
lpcbData
As
Long
)
As
Long
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
,
ByVal
phkResult
As
Long
)
As
Long
Private
Declare
Function
RegSetValueExString
Lib
"advapi32.dll"
Alias
"RegSetValueExA"
(
ByVal
hKey
As
Long
,
ByVal
lpValueName
As
String
,
ByVal
Reserved
As
Long
,
ByVal
dwType
As
Long
,
ByVal
lpValue
As
String
,
ByVal
cbData
As
Long
)
As
Long
Private
Declare
Function
RegSetValueExLong
Lib
"advapi32.dll"
Alias
"RegSetValueExA"
(
ByVal
hKey
As
Long
,
ByVal
lpValueName
As
String
,
ByVal
Reserved
As
Long
,
ByVal
dwType
As
Long
,
ByVal
lpValue
As
Long
,
ByVal
cbData
As
Long
)
As
Long
Private
Declare
Function
RegFlushKey
Lib
"advapi32.dll"
(
ByVal
hKey
As
Long
)
As
Long
'// Registry section definitions
Public
Enum
eHKey
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_CURRENT_CONFIG = &H80000005
HKEY_DYN_DATA = &H80000006
End
Enum
'// Registry value type definitions
Public
Enum
eRegMode
REG_NONE = 0
'// No value type
REG_SZ = 1
'// Unicode nul terminated string
REG_EXPAND_SZ = 2
'// Unicode nul terminated string
REG_BINARY = 3
'// Free form binary
REG_DWORD = 4
'// 32-bit number
REG_DWORD_LITTLE_ENDIAN = 4
'// 32-bit number (same as REG_DWORD)
REG_DWORD_BIG_ENDIAN = 5
'// 32-bit number
REG_LINK = 6
'// Symbolic Link (unicode)
REG_MULTI_SZ = 7
'// Multiple Unicode strings
REG_RESOURCE_LIST = 8
'// Resource list in the resource map
REG_FULL_RESOURCE_DESCRIPTOR = 9
'// Resource list in the hardware description
REG_RESOURCE_REQUIREMENTS_LIST = 10
End
Enum
'// Fonction pour sauver une donné dans le registre
Public
Function
WriteRegistry(
ByVal
hKey
As
eHKey,
ByVal
Section
As
String
,
ByVal
Key
As
String
,
ByVal
RegMode
As
eRegMode,
ByVal
Value
As
Object
)
Dim
KeyValue
As
Long
Call
RegCreateKey(hKey, Section, KeyValue)
'// Ouverture
If
RegMode = REG_DWORD
Then
'// Si c'est une valeur DWORD
Call
RegSetValueExLong(KeyValue, Key, 0&, RegMode,
CLng
(Value), 4)
'// On écrit une valeur de type long
Else
If
RegMode = REG_SZ
Then
Value = Value + Chr(0)
'// Si c une SZ, on ajoute un 0
Call
RegSetValueExString(KeyValue, Key, 0&, RegMode, Value, Len(Value))
'// On écrit une valeur de type texte
End
If
Call
RegFlushKey(KeyValue)
Call
RegCloseKey(KeyValue)
End
Function
'// Fonction pour lire une donnée du registre
Public
Function
ReadRegistry(
ByVal
hKey
As
eHKey,
ByVal
Section
As
String
,
ByVal
Key
As
String
)
As
String
Dim
KeyValue
As
Long
Dim
RegMode
As
eRegMode
On
Error
Resume
Next
Call
RegOpenKey(hKey, Section, KeyValue)
'// Ouverture
ReadRegistry = Space$(2048)
Call
RegQueryValueEx(KeyValue, Key, 0&, RegMode, ReadRegistry, Len(ReadRegistry))
If
Err.Number = 0
Then
If
RegMode = REG_DWORD
Then
'// Si c'est une valeur DWORD
ReadRegistry = Format$(Asc(Mid$(ReadRegistry, 1, 1)) + &H100& * Asc(Mid$(ReadRegistry, 2, 1)) + &H10000 * Asc(Mid$(ReadRegistry, 3, 1)) + &H1000000 *
CDbl
(Asc(Mid$(ReadRegistry, 4, 1))),
"000"
)
'// On converti le format en décimal
ElseIf
RegMode = REG_BINARY
Then
'// Si cest une valeur BINARY
ReadRegistry = Left$(ReadRegistry, Len(ReadRegistry))
'// On lit la valeur
Else
'// Sinon
ReadRegistry = Left$(ReadRegistry, InStr(ReadRegistry, Chr(0)) - 1)
'// lit avant le 0
End
If
End
If
Call
RegCloseKey(KeyValue)
'// Fermeture
End
Function
Et pour l'utilisation:
Call
WriteRegistry(HKEY_CURRENT_USER,
"Software\Microsoft\Windows NT\CurrentVersion\Devices"
,
"\\SrvAub27\Archivage"
, REG_SZ,
"winspool,Ne02:"
)
Kenji
Il était une fois, un pauvre petit règlement que personne ne lisait. Il est tout sympa mais il est triste, aidez-le, lisez-le, ca lui ferait plaisir ainsi qu'a n