СообЧа > База Знаний > Программирование > Visual Basic

Вопрос

Как записать из программы на ВБ информацию в реестр? Имеется в виду в любое место. Желательно привести пример.

Ответ

Посмотри на примере добавления в автозагрузку:

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
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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Sub AddToStartup(ByVal sDesc As String,
ByVal sFile As String)
Dim lRegKey As Long

RegOpenKey HKEY_LOCAL_MACHINE,
"Software\Microsoft\Windows\CurrentVersion\Run",
lRegKey
If lRegKey Then
RegSetValueEx lRegKey,
sDesc, 0, 1, ByVal sFile, Len(sApp)
RegCloseKey lRegKey
End If
End Sub

''Использование

AddToStartup "My App Description", "C:\MyApp\MyApp.exe"


@LEXis

Чтение, запись в реестр Пихаем все это в модуль!!!

Public Const HKEY_CLASSES_ROOT = &H80000000Public
Const HKEY_CURRENT_USER = &H80000001Public
Const HKEY_LOCAL_MACHINE = &H80000002Public
Const HKEY_USERS = &H80000003Public
Const HKEY_PERFORMANCE_DATA = &H80000004Public
Const ERROR_SUCCESS = 0& Declare
Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As LongDeclare
Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long,
ByVal lpSubKey As String, phkResult As Long) As LongDeclare Function
RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long,
ByVal lpSubKey As String)
As LongDeclare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long,
ByVal lpValueName As String)
As LongDeclare Function RegOpenKey Lib "advapi32.dll"
Alias "RegOpenKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long)
As LongDeclare 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 LongDeclare 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 LongPublic Const REG_SZ = 1
' Unicode nul terminated stringPublic Const REG_DWORD = 4
' 32-bit numberPublic Sub savekey(Hkey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(Hkey, strPath, keyhand&)
r = RegCloseKey(keyhand&)End SubPublic Function getstring(Hkey As Long, strPath As String, strValue As String)
Dim keyhand As Long Dim datatype As Long Dim lResult As Long Dim strBuf As String Dim lDataBufSize As Long Dim intZeroPos As Integer
r = RegOpenKey(Hkey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
getstring = Left$(strBuf, intZeroPos — 1)
Else
getstring = strBuf
End If
End If
End If
RegCloseKey keyhandEnd FunctionPublic Sub savestring(Hkey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)End SubFunction getdword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String)
As Long Dim lResult
As Long Dim lValueType
As Long Dim lBuf
As Long Dim lDataBufSize
As Long Dim r As Long Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then getdword = lBuf
End If
End If
r = RegCloseKey(keyhand)End
FunctionFunction SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim lResult As Long Dim keyhand As Long Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
r = RegCloseKey(keyhand)End FunctionPublic Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String) Dim r As Long
r = RegDeleteKey(Hkey, strKey)End
FunctionPublic Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String) Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)End Function


А как применять сами разберетесь :)

Ministr



Copyright © 2000-2004 Сообщество Чайников
Контактная информация