Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

registry problem

i'm trying to change the registry but for some reason, that i don't know, the following code is not working. Can you help?
I've tryed several things but none of them worked!

This is the button on the form:

Private Sub cmdProcessar_Click()
Dim keyValue As String, keyName As String
Dim retValue As Long, keyID As Long, hKey As Long
Dim KeyValType As Long
Dim tmpVal As String
Dim KeyValSize As Long

retValue = RegCreateKey(HKEY_LOCAL_MACHINE, CONFIGURACAO_REG_KEY, keyID)
keyName = "SharingViolationDelay"

keyValue = 1

retValue = RegSetValueEx(keyID, keyName, 0&, REG_DWORD, ByVal keyValue, 1)
keyName = "SharingViolationRetries"
keyValue = 1
retValue = RegSetValueEx(keyID, keyName, 0&, REG_DWORD, ByVal keyValue, 1) 'Len(keyValue) + 1)
'retValue = RegCloseKey(hKey)
If retValue <> 0 Then
MsgBox "Processo concluido sem sucesso!", vbInformation, "Gestrato Software"
Else
MsgBox "Processo concluido com sucesso!", vbInformation, "Gestrato Software"
End If

End Sub


this is the module:

Public Const ERROR_SUCCESS = 0&
Public Const ERROR_BADDB = 1009&
Public Const ERROR_BADKEY = 1010&
Public Const ERROR_CANTOPEN = 1011&
Public Const ERROR_CANTREAD = 1012&
Public Const ERROR_CANTWRITE = 1013&
Public Const ERROR_REGISTRY_RECOVERED = 1014&
Public Const ERROR_REGISTRY_CORRUPT = 1015&
Public Const ERROR_REGISTRY_IO_FAILED = 1016&
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
'Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit number


Public Const STOCKS_OPCOES_REG_KEY = "SoftwareGestrato StocksOpcoes"
Public Const CONFIGURACAO_REG_KEY = "SYSTEMCurrentControlSetServiceslanlanserverParameters"

Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.


My thanks in advanced

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : i'm trying to change the registry but for some reason, that i don't
    : know, the following code is not working. Can you help?
    : I've tryed several things but none of them worked!
    :

    Check the return values of the functions to determine where it goes wrong (ie where ERROR_SUCCESS is not returned).

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • RicardoFurtadoRicardoFurtado Member Posts: 205
    : : i'm trying to change the registry but for some reason, that i don't
    : : know, the following code is not working. Can you help?
    : : I've tryed several things but none of them worked!
    : :
    :
    : Check the return values of the functions to determine where it goes
    : wrong (ie where ERROR_SUCCESS is not returned).
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    Thank you for your reply.
    I've check and they return 0. Thats what confuses me the most, because it seems that it should be working

    Best Regards
    Ricardo Furtado
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Thank you for your reply.
    : I've check and they return 0. Thats what confuses me the most,
    : because it seems that it should be working
    :
    : Best Regards
    : Ricardo Furtado

    So I take it the keys do not appear in the registry? Even if you restart the computer?

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • RicardoFurtadoRicardoFurtado Member Posts: 205
    : : Thank you for your reply.
    : : I've check and they return 0. Thats what confuses me the most,
    : : because it seems that it should be working
    : :
    : : Best Regards
    : : Ricardo Furtado
    :
    : So I take it the keys do not appear in the registry? Even if you
    : restart the computer?
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    If they exist, they do not change their values to 1, if they don't exist, then they do not appear in the registry. Its like nothing happened

    Best Regards
    Ricardo Furtado
  • HackmanCHackmanC Member Posts: 441
    Make sure all your strings are 0 (null) terminated strings.
    The size of the key is 4 bytes (Long / DWord).
    Close the correct handle to the key.

    [code]
    Dim CONFIGURACAO_REG_KEY As String
    CONFIGURACAO_REG_KEY = "SYSTEMCurrentControlSetServiceslanlanserverParameters"[color=Red] & Chr$(0)[/color]

    retValue = RegSetValueEx(keyID, keyName, 0&, REG_DWORD, ByVal keyValue, [color=Red]4[/color])

    retValue = RegCloseKey(keyID)
    [/code]

    After those changes your application changes the values or creates new keys.
    If still doesn't work, the problem isn't in your code.
    Check the permission in the key for the user in regedit, etc...

    btw... WinXp sp 2, vb 6 sp 5.
    (Press F5 on regedit to watch the change)

    [red]Good luck![/red]
    [blue]Hackman[/blue]
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Make sure all your strings are 0 (null) terminated strings.

    VB strings are 0-terminated, when calling the Windows API's, so there's no problems there.

    I think I see what's wrong now: You're using a String to set a DWORD value (that's bad). But that's not why it's not working (even though this needs to be improved too). The length of cbData must neccesarily be 4 bytes, and you are telling the API you're only passing one byte to it.

    So, what you do is you make a Long variable (which has the same size as a DWORD), and pass that to the function (without the explicit ByVal), and you set the size to 4 instead of 1 (or, more generally to: LenB(dwordKeyValue)).

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.