Con este modulo vamos a poder incorporar a nuestras aplicaciones la capacidad de poder agregar, eliminar y modificar valores al registro de Windows con gran facilidad. Además también la posibilidad de hacer que nuestros programas posean la capacidad de iniciar con Windows. Constantes Son identificar para a las cuatro ramas principales del registro, mas los distintos de datos que podemos manejar. dijo:Public Const HKEY_CLASSES_ROOT = &H80000000 Public Const HKEY_CURRENT_USER = &H80000001 Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const HKEY_USERS = &H80000003 Public Const REG_SZ As Long = 1 Public Const REG_DWORD As Long = 4 Public Const KEY_ALL_ACCESS = &H3F Public Const REG_OPTION_NON_VOLATILE = 0 Declaración de las API's. Cada declaración tiene a su lado un comentario aclarando su función. dijo: ' Cierra la clave abierta Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long ) As Long 'Abre una clave 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 , _ phkResult As Long ) As Long 'Establece un valor de tipo cadena 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 'Establece un valor de tipo entero 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 , lpValue As Long , ByVal _ cbData As Long ) As Long 'Elimina una clave Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias _ "RegDeleteKeyA" (ByVal hKey As Long , ByVal lpSubKey As String) 'Elimina un valor del registro Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias _ "RegDeleteValueA" (ByVal hKey As Long , ByVal lpValueName As String) Variables Globales dijo: Const RAMA_RUN_WINDOWS As String = _ "SOFTWAREMicrosoftWindowsCurrentVersionRun" Dim Clavess As String Dim SECCION As String Registrar/Desregistrar Aplicación Esta Función nos permite tanto registrar o quitar nuestra aplicación del registro para que se inicie con Windows. Esta función tiene como parámetro formal a la variable Registrar que es boleen, Si tiene valor verdadero entonces registra al programa y es falso hace lo contrario. Si la función tiene existo devuelve verdadero. dijo: Function registrar_y_desregistrar(Registrar As Boolean) Const Path_Programa = App.Path & "" & App.EXEName & ".exe" Const Titulo_Programa = App.Title Dim Ret As Boolean If Registrar Then Ret = EstablecerValor(HKEY_LOCAL_MACHINE, RAMA_RUN_WINDOWS, _ Titulo_Programa, Path_Programa, REG_SZ) Else Ret = EliminarValor(HKEY_LOCAL_MACHINE, RAMA_RUN_WINDOWS, _ Titulo_Programa) End If registrar_y_desregistrar = Ret End Function Establecer Valor Esta función establece algún valor en el registro, la tiene cinco valores de entradas Clave, Nombre_clave, Nombre_clave, Nombre_clave, Tipo_Valor . Para llamar a esta función en los campos del tipo long tenemos que utilizar las constantes ya que estas contiene los valores definidos por el SO. Los únicos campos que debemos especificar en especial nosotros son los restantes. Si tiene exito devuelve verdadero. dijo: Function EstablecerValor(Clave As Long, Nombre_clave As String, _ Nombre_clave As String, Nombre_clave As Variant, Tipo_Valor As Long) _ As Boolean Dim Ret As Long Dim Handle_clave As Long Ret = RegOpenKeyEx(Clave, Nombre_clave, 0, KEY_ALL_ACCESS, _ Handle_clave) If Ret <> 0 Then EstablecerValor = False Exit Function End If Ret = SetValueEx(Handle_clave, Nombre_valor, Tipo_Valor, el_Valor) If Ret <> 0 Then EstablecerValor = False Exit Function End If RegCloseKey (Handle_clave) EstablecerValor = True End Function Private Function SetValueEx(ByVal Handle_clave As Long, Nombre_valor _ As String, Tipo As Long, el_Valor As Variant) As Long Dim Ret As Long Dim sValue As String Select Case Tipo Case REG_SZ sValue = el_Valor SetValueEx = RegSetValueExString(Handle_clave, Nombre_valor, _ 0&, Tipo, sValue, Len(sValue)) Case REG_DWORD Ret = el_Valor SetValueEx = RegSetValueExLong(Handle_clave, Nombre_valor, 0&,_ Tipo, Ret, 4) End Select End Function Eliminar Valor Esta función elimina algún valor en el registro, la tiene tres valores de entradas Clave, Nombre_clave, Nombre_clave, Nombre_valor. Para llamar a esta función en los campos del tipo long tenemos que utilizar las constantes ya que estas contiene los valores definidos por el SO. Los únicos campos que debemos especificar en especial nosotros son los restantes. Si tiene exito devuelve verdadero. dijo: Function EliminarValor(Clave As Long, Nombre_clave As String, _ Nombre_valor As String) As Boolean Dim Ret As Long Dim Handle_clave As Long Ret = RegOpenKeyEx(Clave, Nombre_clave, 0, KEY_ALL_ACCESS, _ Handle_clave) If Ret <> 0 Then EliminarValor = False Exit Function End If Ret = RegDeleteValue(Handle_clave, Nombre_valor) If Ret <> 0 Then EliminarValor = False Exit Function End If RegCloseKey (Handle_clave) EliminarValor = True End Function Funciones de Configuración Rápida Estas Funciones nos permiten guardar en el registro las configuraciones de nuestras aplicaciones de forma rápida, y también podemos recuperar esos datos de forma rápida. Son bastantes fáciles de usar y aumentaran tus posibilidades al programar en Visual Basic. Para poderlas usar solo debemos llamar a las funciones Obtener_Valor, Cambiar_Valor, Eliminar_Valor. dijo: Function EliminarClave(Clave As Long, Nombre_clave As String) Dim Ret As Long Ret = RegDeleteKey(Clave, Nombre_clave) End Function dijo: Public PropertyGet Valor() As String Valor = GetSetting(App.EXEName, SECCION, Clavess, "" ) End Property dijo: Public Property Let Valor(ByVal valor_1 As String) Call SaveSetting(App.EXEName, SECCION, Clavess, valor_1) End Property dijo: Function Eliminar_Valor(Clave As String) If Valor = vbNullString Then Exit Function End If Call DeleteSetting(App.EXEName, SECCION, Clavess) End Function dijo: Function Cambiar_Valor(New_Valor As String, P As String, Q As String) Clavess = P SECCION = Q Valor = New_Valor End Function dijo: Function Obtener_Valor(P As String, Q As String) Clavess = P SECCION = Q If Valor = vbNullString Then Obtener_Valor = 0 Else Obtener_Valor = Valor End If End Function Ejemplo Este ejemplo dará una visión más clara de como usar él modulo. Y de acá tambien podes bajar él modulo. Ejemplo Bajar Ejemplo Bajar Modulo FUENTE
Modulo Para el Manejo Del Registro [Visual Basic]
Datos archivados del Taringa! original
28puntos
1,214visitas
0comentarios
Actividad nueva en Posteamelo
0puntos
3visitas
0comentarios
Dar puntos: