elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Problemas al leer el registro de Windows
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problemas al leer el registro de Windows  (Leído 3,005 veces)
aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Problemas al leer el registro de Windows
« en: 20 Septiembre 2008, 13:39 pm »

Hola. Estoy usando un módulo de clase que conseguí en RecursosVisualBasic que me permite modificar el registro. Las claves las crea perfectamente, pero no soy capaz de leerlas. Este es el código:

Código
  1. Option Explicit
  2.  
  3. 'Declaración de constantes
  4. '****************************
  5. Private Const REG_SZ As Long = 1
  6. Private Const REG_DWORD As Long = 4
  7.  
  8. Private Const ERROR_NONE = 0
  9. Private Const ERROR_BADDB = 1
  10. Private Const ERROR_BADKEY = 2
  11. Private Const ERROR_CANTOPEN = 3
  12. Private Const ERROR_CANTREAD = 4
  13. Private Const ERROR_CANTWRITE = 5
  14. Private Const ERROR_OUTOFMEMORY = 6
  15. Private Const ERROR_INVALID_PARAMETER = 7
  16. Private Const ERROR_ACCESS_DENIED = 8
  17. Private Const ERROR_INVALID_PARAMETERS = 87
  18. Private Const ERROR_NO_MORE_ITEMS = 259
  19. Private Const KEY_ALL_ACCESS = &H3F
  20. Private Const REG_OPTION_NON_VOLATILE = 0
  21.  
  22. 'Declaración de las funciones api para el registro
  23. '*************************************************
  24.  
  25. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  26. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
  27.       (ByVal hKey As Long, _
  28.        ByVal lpSubKey As String, _
  29.        ByVal Reserved As Long, _
  30.        ByVal lpClass As String, _
  31.        ByVal dwOptions As Long, _
  32.        ByVal samDesired As Long, _
  33.        ByVal lpSecurityAttributes As Long, _
  34.        phkResult As Long, _
  35.        lpdwDisposition As Long) As Long
  36.  
  37. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
  38.       (ByVal hKey As Long, _
  39.        ByVal lpSubKey As String, _
  40.        ByVal ulOptions As Long, _
  41.        ByVal samDesired As Long, _
  42.        phkResult As Long) As Long
  43.  
  44. Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" _
  45.       (ByVal hKey As Long, _
  46.        ByVal lpValueName As String, _
  47.        ByVal lpReserved As Long, _
  48.        lpType As Long, _
  49.        ByVal lpData As String, _
  50.        lpcbData As Long) As Long
  51.  
  52. Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" _
  53.       (ByVal hKey As Long, _
  54.        ByVal lpValueName As String, _
  55.        ByVal lpReserved As Long, _
  56.        lpType As Long, _
  57.        lpData As Long, _
  58.        lpcbData As Long) As Long
  59.  
  60. Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" _
  61.       (ByVal hKey As Long, _
  62.        ByVal lpValueName As String, _
  63.        ByVal lpReserved As Long, _
  64.        lpType As Long, _
  65.        ByVal lpData As Long, _
  66.        lpcbData As Long) As Long
  67.  
  68. Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" _
  69.        (ByVal hKey As Long, _
  70.         ByVal lpValueName As String, _
  71.         ByVal Reserved As Long, _
  72.         ByVal dwType As Long, _
  73.         ByVal lpValue As String, _
  74.         ByVal cbData As Long) As Long
  75. Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
  76.         "RegSetValueExA" (ByVal hKey As Long, _
  77.         ByVal lpValueName As String, _
  78.         ByVal Reserved As Long, _
  79.         ByVal dwType As Long, _
  80.         lpValue As Long, _
  81.         ByVal cbData As Long) As Long
  82.  
  83. Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  84.        (ByVal hKey As Long, _
  85.         ByVal lpSubKey As String)
  86.  
  87. Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" _
  88.        (ByVal hKey As Long, _
  89.         ByVal lpValueName As String)
  90.  
  91.  
  92. 'Funciones públicas para crear, eliminar, consultar los datos
  93. '****************************************************************
  94. ' Función que elimina una clave especifica utilizando el Api RegDeleteKey
  95. Public Function EliminarClave(clave As Long, Nombre_clave As String)
  96.    Dim ret As Long
  97.    ret = RegDeleteKey(clave, Nombre_clave)
  98. End Function
  99. ' Función que elimina un dato utilizando el Api RegDeleteValue
  100. Public Function EliminarValor(clave As Long, _
  101.                              Nombre_clave As String, _
  102.                              Nombre_valor As String)
  103.       Dim ret As Long
  104.       Dim Handle_clave As Long
  105.  
  106.       ' Abre la clave del registro
  107.       ret = RegOpenKeyEx(clave, Nombre_clave, 0, KEY_ALL_ACCESS, Handle_clave)
  108.       'Elimina el valor del registro
  109.       ret = RegDeleteValue(Handle_clave, Nombre_valor)
  110.       'Cierra la vlave del registro abierta
  111.       RegCloseKey (Handle_clave)
  112. End Function
  113.  
  114. ' Función que crea una nueva Clave utilizando el Api RegCreateKeyEx
  115. Public Function CrearNuevaClave(clave As Long, Nombre_clave As String)
  116.  
  117.    Dim Handle_clave As Long
  118.    Dim ret As Long
  119.  
  120.    ret = RegCreateKeyEx(clave, _
  121.                         Nombre_clave, 0&, vbNullString, _
  122.                         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, _
  123.                         Handle_clave, ret)
  124.  
  125.    RegCloseKey (Handle_clave)
  126.  
  127. End Function
  128. ' Función que establece un nuevo valor mediante el Api SetValueEx
  129.  
  130. Public Function EstablecerValor(clave As Long, _
  131.                                Nombre_clave As String, _
  132.                                Nombre_valor As String, _
  133.                                el_Valor As Variant, _
  134.                                Tipo_Valor As Long)
  135.  
  136.  
  137.       Dim ret As Long
  138.       Dim Handle_clave As Long
  139.  
  140.       ret = RegOpenKeyEx(clave, Nombre_clave, 0, KEY_ALL_ACCESS, Handle_clave)
  141.       ret = SetValueEx(Handle_clave, Nombre_valor, Tipo_Valor, el_Valor)
  142.  
  143.       RegCloseKey (Handle_clave)
  144.  
  145. End Function
  146. ' Función que consulta un dato del registro usando QueryValueEx
  147. Public Function ConsultarValor(clave As Long, Nombre_clave As String, Nombre_valor As String)
  148.  
  149.       Dim Handle_clave As Long
  150.       Dim valor As Variant
  151.  
  152.       Dim ret As Long
  153.  
  154.       ret = RegOpenKeyEx(clave, Nombre_clave, 0, KEY_ALL_ACCESS, Handle_clave)
  155.  
  156.       ret = QueryValueEx(Handle_clave, Nombre_valor, valor)
  157.       ' REtorna el valor del registro a la función
  158.       ConsultarValor = valor
  159.       'Cierra la clave abierta del registro
  160.       RegCloseKey (Handle_clave)
  161. End Function
  162.  
  163. ' Funciones privadas del módulo
  164. Private Function SetValueEx(ByVal Handle_clave As Long, _
  165.                            Nombre_valor As String, _
  166.                            Tipo As Long, _
  167.                            el_Valor As Variant) As Long
  168.  
  169.    Dim ret As Long
  170.    Dim sValue As String
  171.  
  172.    Select Case Tipo
  173.  
  174.        ' Valor de tipo cadena
  175.        Case REG_SZ
  176.  
  177.            sValue = el_Valor
  178.            SetValueEx = RegSetValueExString(Handle_clave, _
  179.                                             Nombre_valor, 0&, _
  180.                                             Tipo, sValue, Len(sValue))
  181.  
  182.        'Valor Entero
  183.        Case REG_DWORD
  184.            ret = el_Valor
  185.            SetValueEx = RegSetValueExLong(Handle_clave, Nombre_valor, 0&, Tipo, ret, 4)
  186.        End Select
  187. End Function
  188. Private Function QueryValueEx(ByVal lhKey As Long, _
  189.                              ByVal Name_Valor As String, _
  190.                              el_Valor As Variant) As Long
  191.    Dim cch As Long
  192.    Dim lrc As Long
  193.    Dim Tipo As Long
  194.    Dim ret_Valor As Long
  195.    Dim dato As String
  196.    On Error GoTo QueryValueExError
  197.    lrc = RegQueryValueExNULL(lhKey, Name_Valor, 0&, Tipo, 0&, cch)
  198.    If lrc <> ERROR_NONE Then Error 5
  199.    Select Case Tipo
  200.        Case REG_SZ:
  201.            dato = String(cch, 0)
  202.            lrc = RegQueryValueExString(lhKey, Name_Valor, 0&, Tipo, dato, cch)
  203. If lrc = ERROR_NONE Then
  204.                el_Valor = Left$(dato, cch)
  205. Else
  206.                el_Valor = Empty
  207. End If
  208.        Case REG_DWORD:
  209.            lrc = RegQueryValueExLong(lhKey, Name_Valor, 0&, Tipo, ret_Valor, cch)
  210.            If lrc = ERROR_NONE Then el_Valor = ret_Valor
  211.        Case Else
  212.            lrc = -1
  213.    End Select
  214. QueryValueExExit:
  215.    QueryValueEx = lrc
  216.    Exit Function
  217. QueryValueExError:
  218.    Resume QueryValueExExit
  219. End Function

Yo utilizo:
Código
  1. MsgBox cRegistro.ConsultarValor(&H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\", "")

¿Alguien sabe por qué puede ser? Gracias de antemano.


En línea

Spider-Net


Desconectado Desconectado

Mensajes: 1.165


Un gran poder conlleva una gran responsabilidad


Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #1 en: 20 Septiembre 2008, 13:45 pm »

¿Y por qué te complicas tanto?, mira yo siempre que quiero leer alguna rama del registro, lo hago así... por ejemplo:

Código
  1. Set Objeto = CreateObject("wscript.shell")
  2. LeerReg = Objeto.RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\")
  3. If LeerReg = "loquesea" Then
  4. 'Aquí las instrucciones a realizar
  5. End If
  6.  


En línea

aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #2 en: 20 Septiembre 2008, 13:46 pm »

Pero a mí no me gusta utilizar el WSH, prefiero utilizar APIs para esto.

Saludos.
En línea

Spider-Net


Desconectado Desconectado

Mensajes: 1.165


Un gran poder conlleva una gran responsabilidad


Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #3 en: 20 Septiembre 2008, 13:48 pm »

Ahhh, ok. Yo es que siempre lo hice así ;)

Oye y esto:

Código
  1. MsgBox cRegistro.ConsultarValor(&H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\", "")

No debería de ser?


Código
  1. MsgBox cRegistro.ConsultarValor(&H80000002, "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "")

??? Yo pregunto eh? porque nunca lo he hecho con api's la verdad..

Saludos!
« Última modificación: 20 Septiembre 2008, 13:50 pm por Spider-Net » En línea

aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #4 en: 20 Septiembre 2008, 14:06 pm »

No, porque HKEY_LOCAL_MACHINE es la constante &H80000002, que viene en el argumento anterior.

Saludos.
En línea

Lambda


Desconectado Desconectado

Mensajes: 371



Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #5 en: 20 Septiembre 2008, 14:35 pm »

No, porque HKEY_LOCAL_MACHINE es la constante &H80000002, que viene en el argumento anterior.

Saludos.

Prueba esta clase http://www.elguille.info/vb/utilidades/QUERYREG/QueryReg5.htm

La he usado muchas veces y 0 problemas  ;D
En línea

aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #6 en: 20 Septiembre 2008, 14:52 pm »

Lo malo es que para lo que yo quiero hacer, necesito que el tamaño del módulo donde venga sea pequeño, ya que la clase de elguille ocupa unos 90Kb.

Saludos.
En línea

byway

Desconectado Desconectado

Mensajes: 181


^^,


Ver Perfil
Re: Problemas al leer el registro de Windows
« Respuesta #7 en: 20 Septiembre 2008, 17:15 pm »

En el modulo no veo la constante declarada:

Código
  1. Public Const HKEY_LOCAL_MACHINE = &H80000002

ademas:
Código
  1. MsgBox cRegistro.ConsultarValor(&H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\", "")

no seria asi:
Código
  1. MsgBox cRegistro.ConsultarValor(HKEY_LOCAL_MACHINE,"Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "")

y por ultimo me parece esta incompleto la llamada para leer el valor:

Código
  1. Public Function ConsultarValor(clave As Long, Nombre_clave As String, Nombre_valor As String)

al parecer te falta el nombre del valor... por si te fijas debe ir uno y no dejarlo en blanco...

Aki un ejemplo.. me parece es el mismo que estas usando, pero este esta mas entendible :

http://www.recursosvisualbasic.com.ar/htm/listado-api/132-registro-de-windows.htm

Salu2.






« Última modificación: 20 Septiembre 2008, 17:22 pm por byway » En línea

aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Re: Problemas al leer el registro de Windows
« Respuesta #8 en: 20 Septiembre 2008, 19:17 pm »

¿Para que voy a declarar la constante si ya tengo su valor?

Creo que el último argumento es por si no existe el valor, para ponerlo por defecto. El ejemplo que pones tú es el mismo que tengo yo, así que seguimos en la misma.

Saludos.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Leer el valor de una key del registro de windows
Programación C/C++
extreme69 6 6,721 Último mensaje 20 Febrero 2011, 06:02 am
por extreme69
Problemas para arreglar el registro de Windows
Windows
furby1194 5 4,562 Último mensaje 11 Julio 2011, 16:24 pm
por zeroteck
Leer el registro de windows?
Programación C/C++
KroSaver 2 3,849 Último mensaje 11 Agosto 2011, 03:16 am
por kiriost
por favor es urgente registro de windows con problemas
Windows
bolli 6 2,472 Último mensaje 12 Noviembre 2012, 18:41 pm
por bolli
Leer registro de excepciones del firewall de windows
Programación C/C++
33boy33 1 1,712 Último mensaje 23 Abril 2014, 05:05 am
por x64core
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines