Hola en el proyecto actual que estoy haciendo estoy con el registro de windows, tengo algun que otro problemilla al leer los valores, cuando los valores son DWORD, no consigo leer su valor, les pongo el codigo:
Public Function ValueData(Key As Long, strValueName As String, sType As Long) As String
Dim lResult As Long, RetornoData As Long, strData As String, lngData As Currency, binData() As Byte, sTmp As String
Dim i As Integer
Select Case sType
Case REG_NONE
ValueData = vbNullString
Case REG_SZ, REG_EXPAND_SZ, REG_LINK
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal 0, RetornoData)
If lResult = 0 Then
strData = String(RetornoData, Chr$(0))
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal strData, RetornoData)
If lResult = 0 Then
ValueData = Left$(strData, InStr(1, strData, Chr$(0)) - 1)
End If
End If
Case REG_MULTI_SZ
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal 0, RetornoData)
If lResult = 0 Then
strData = String(RetornoData, Chr$(0))
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal strData, RetornoData)
If lResult = 0 Then
For i = Len(strData) To 1 Step -1
If Mid$(strData, i, 1) = Chr$(0) Then
strData = Left$(strData, i - 1)
Exit For
End If
Next
For i = 1 To Len(strData)
If Mid$(strData, i, 1) = Chr$(0) Then
Mid$(strData, i, 1) = " "
End If
Next
ValueData = Left$(strData, InStr(1, strData, Chr$(0)) - 1)
End If
End If
For i = Len(strData) To 1 Step -1
If Mid$(strData, i, 1) = Chr$(0) Then
strData = Left$(strData, i - 1)
Exit For
End If
Next
For i = 1 To Len(strData)
If Mid$(strData, i, 1) = Chr$(0) Then
Mid$(strData, i, 1) = " "
End If
Next
Case REG_BINARY
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal 0, RetornoData)
If lResult = 0 Then
ReDim binData(RetornoData)
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, binData(0), RetornoData)
If lResult = 0 Then
For i = 0 To UBound(binData) - 1
sTmp = sTmp & Right$("00" & Hex$(binData(i)), 2) & " "
Next
ValueData = sTmp
End If
End If
Case REG_DWORD, REG_DWORD_BIG_ENDIAN
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, lngData, RetornoData)
If lResult = 0 Then
ValueData = "0x" & Right$("00000000" & Hex$(lngData), 8) & " (" & lngData & ")"
End If
Case Else
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal 0, RetornoData)
If lResult = 0 Then
strData = String(RetornoData, Chr$(0))
lResult = RegQueryValueEx(Key, strValueName, 0, ByVal 0, ByVal strData, RetornoData)
If lResult = 0 Then
ValueData = Left$(strData, InStr(1, strData, Chr$(0)) - 1)
End If
End If
End Select
End Function
A la funcion se la llama con la clave ya abierta, si pueden ayudarme se lo agradeceria.
PD: ¿Como hacen para que el codigo salga con colores? Es menos lioso xD
Encerra el codigo dentro de las etiquetas con el lenguaje correspondiente, por ejemplo en este caso con [code =vb] ' codigo [ /code] pero sin los espacios y veras que queda coloreado
Ok eso responde a mi pregunta del color del codigo (no lo corrijo ahora porque con el movil tardo mucho ;D ), pero volviendo al tema de mi pregunta podriais ayudarme con los valores DWORD?
Yo no tengo ni idea, pero buscate en google como leer valores Dword, o sino capas que si buscas bien hasta encontras una forma de que lea todo.
En primer lugar tienes que asegurarte que el valor RetornoData es 4 o más grande, ya que para guardar un valor DWORD se necesitan 4 bytes, si no, RegQueryValueEx no te retornará ERROR_SUCCESS.
Para los valores de cadena y binarios, ya haces esa comprobación llamando 2 veces al api, para los DWORD no hace falta poque SIEMPRE van a ser 4 bytes
por otro lado, los valores DWORD son números de 32 bits (4bytes) SIN signo, mientras que los valores Long de vb, son números de 32 bits CON signo.
Esto quiere decir que un valor WORD puede ir
de 0 0x 00 00 00 00
a 4294967295 0x FF FF FF FF
y un Long puede ir
de 0 0x 00 00 00 00
a 2147483647 0x 07 FF FF FF
y de -1 0x FF FF FF FF
a -2147483648 0x 08 FF FF FF
por lo que si el long es negativo, hay que hacer una conversión.
Dim ValueData As String
Dim hKey As Long
If RegOpenKey(HKEY_CURRENT_USER, "kk", hKey) = ERROR_SUCCESS Then
Dim lngData As Long 'un valor Long es un número de 4 bytes CON signo
If RegQueryValueEx(hKey, "Nuevo", ByVal 0, ByVal 0, lngData, 4) = ERROR_SUCCESS Then
If lngData < 0 Then 'Si el long es negativo se pasa a DWORD (por ejemplo asi)
ValueData = "0x" & Right$("00000000" & Hex$(lngData), 8) & " (" & (2 * 2147483648#) + lngData & ")"
Else
ValueData = "0x" & Right$("00000000" & Hex$(lngData), 8) & " (" & lngData & ")"
End If
End If
RegCloseKey hKey
End If
Saludos.
Muchas gracias me has aclarado mucho, muy bien explicado tambien. Mañana mismo corregire el codigo.
Gracias de nuevo burbu_1 ;D