Results 1 to 9 of 9

Thread: Need help with writing to registry please

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    18

    Need help with writing to registry please

    Hey, im trying to write a new value to the registry using the shell method, ill explain more after i show u what im currently doing.

    Code:
    ApplicationPath = App.Path & "\" & App.EXEName & ".exe"
    
    RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" & _
                  "SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List\"
    value = ApplicationPath
    
    Dim Shell As Object
    
        Set Shell = CreateObject("wscript.shell")
        On Error Resume Next
    
        Shell.RegWrite RegPath & value, ApplicationPath & ":*:Enabled:" & App.EXEName, "REG_SZ" 
        
        If Err.Number <> 0 Then
        Else
        End If
        On Error GoTo 0
    Now wat i wanted it to do was add the program to the exceptions list but instead, inside the registry after List it creates C:\ > Documents and Settings \ > Intesive \> etc etc, breaking up every part of my ApplicationPath into a seperate folder, when all i want to do is make the value name: ApplicationPath and the value data: ApplicationPath & ":*:Enabled:" & App.EXEName

    Sorry if this is a mouthfull but i appear to be getting nowhere

    Any help appreciated.

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Need help with writing to registry please

    Scripting host is a bit limited, as it uses the root key, subkey and value name in a continuous string format. Any backslashes are regarded as delimiters.

    Use WMI or the APIs for this.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    18

    Re: Need help with writing to registry please

    im unfamiliar, do you have any links please

  4. #4
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Need help with writing to registry please

    WMI is probably easiest. Here's a snippet taken from a larger app to give you the idea (some bits and pieces may be missing)

    vb Code:
    1. Const WINMGMTS = "winmgmts:{impersonationLevel=impersonate}!\\"
    2. Const STDREGPROV = "\root\default:StdRegProv"
    3. Const strComputer = "."
    4. Const ERROR_FAILED = -999&
    5. Const ERROR_SUCCESS = 0&
    6.  
    7. Public Function WmiWriteSZ(strComputer As String, lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
    8. 'Create a key and respective REG_SZ value from a simple string
    9. 'eg:- "Hello World".
    10.    Dim objReg As Object
    11.  
    12. 'Set the default return value.
    13.    WmiWriteSZ = ERROR_FAILED
    14. 'Create the WMI StdRegProv object.
    15.    Set objReg = GetObject(WINMGMTS & strComputer & STDREGPROV)
    16. 'Create the subkey.
    17.    If objReg.CreateKey(lngHKey, strSubKey) = ERROR_SUCCESS Then
    18. 'Write it to the registry, and return a code.
    19.       WmiWriteSZ = objReg.SetStringValue(lngHKey, strSubKey, strValueName, strValue)
    20.    End If
    21. End Function

    Also see http://msdn2.microsoft.com/en-us/library/aa394573.aspx

    Personally, I prefer using the APIs. If you search my previous posts, you'll find examples for dealing with various data tyoes.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    18

    Re: Need help with writing to registry please

    Hey, So this is the code that i want to use to test if it does what i want:

    vb Code:
    1. Const WINMGMTS = "winmgmts:{impersonationLevel=impersonate}!\\"
    2. Const STDREGPROV = "\root\default:StdRegProv"
    3. Const strComputer = "."
    4. Const ERROR_FAILED = -999&
    5. Const ERROR_SUCCESS = 0&
    6.  
    7. Public Function WmiWriteSZ(strComputer As String, lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
    8.  
    9.         lngHKey = "HKEY_LOCAL_MACHINE"
    10.         strSubKey = "\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List\"
    11.         strValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
    12.         strValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
    13.  
    14.  
    15.   'Create a key and respective REG_SZ value from a simple string
    16.   'eg:- "Hello World".
    17.         Dim objReg As Object
    18.   'Set the default return value.
    19.         WmiWriteSZ = ERROR_FAILED
    20.  'Create the WMI StdRegProv object.
    21.         Set objReg = GetObject(WINMGMTS & strComputer & STDREGPROV)
    22.  'Create the subkey.
    23.         If objReg.CreateKey(lngHKey, strSubKey) = ERROR_SUCCESS Then
    24.  'Write it to the registry, and return a code.
    25.         WmiWriteSZ = objReg.SetStringValue(lngHKey, strSubKey, strValueName, strValue)
    26.    End If
    27. End Function

    Is what i did for lngHKey and strSubKey in the correct order / format?

    I put this inside a module..is that what your supposed to do, either way im unsure how to run this procedure because whenever in Command1_Click i put WmiWriteSZ, it comes up with an argument error. Can you please tell me how to actually run this function.

    Thanks for your time so far busdriver

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Need help with writing to registry please

    Caution: WMI is not an "application rated" service.

    For one thing the WMI Service may be intentionally disabled on many machines. For another it often uses voodoo to get "right some of the time" answers.

    How do I obtain the computer manufacturer's name?

    WMI is a support tool and not meant for use in applications.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    18

    Re: Need help with writing to registry please

    so how can i name a registry key a path name without it creating more sub keys?

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Need help with writing to registry please

    @ dilettante. Good point. The same is true of WSH and regedit - which can be disabled and may not even be installed in older operating systems. (which is why I prefer APIs)

    @ Remmacs.Intensive. The error was because lngHKey should have been a Long (I did say some bits and pieces may be missing )
    ie to call:
    vb Code:
    1. Const HKEY_LOCAL_MACHINE = &H80000002 '<<<--------
    2. Const MySubKey = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List"
    3. Const MyValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
    4. Const MyValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
    5.  
    6. Private Sub Form_Click()
    7.    MsgBox WmiWriteSZ(strComputer, HKEY_LOCAL_MACHINE, MySubKey, MyValueName, MyValue)
    8. End Sub
    The following is an API solution. As you may guess, there are several ways of doing this using different APIs. It's slightly more complex than WMI or WSH, but at least it's guaranteed.
    vb Code:
    1. Option Explicit
    2.  
    3. 'Creates the specified registry key if necessary.
    4. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    5.    (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
    6.    ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    7.    lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
    8.  
    9. 'Opens the specified registry key.
    10. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    11.    (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    12.    ByVal samDesired As Long, phkResult As Long) As Long
    13.  
    14. 'Sets the data and type of a specified value under a registry key.
    15. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    16.    (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    17.    ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    18. 'Note that if you declare the lpData parameter as String, you must pass it By Value.
    19.  
    20. 'Closes a handle to the specified registry key.
    21. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
    22.  
    23. 'Error handling.
    24. Private Const ERROR_FAILED = -999& 'Custom.
    25. Private Const ERROR_SUCCESS = 0&
    26.  
    27. 'Registry manipulation
    28. Private Const REG_OPTION_NON_VOLATILE = 0
    29. Private Const KEY_CREATE_SUB_KEY = &H4
    30. Private Const KEY_SET_VALUE = &H2
    31. Private Const REG_SZ = 1
    32.  
    33. 'Other stuff.
    34. Private Const HKEY_LOCAL_MACHINE = &H80000002
    35. Private Const MySubKey = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List"
    36. Private Const MyValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
    37. Private Const MyValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
    38.  
    39. Private Sub Form_Click()
    40.    MsgBox RegWriteSZ(HKEY_LOCAL_MACHINE, MySubKey, MyValueName, MyValue)
    41. End Sub
    42.  
    43. Private Function RegWriteSZ(lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
    44. 'Create a key and respective REG_SZ value from a simple string
    45. 'eg:- "Hello World".
    46.    Dim lngRetval     As Long
    47.    Dim lngKeyHandle  As Long
    48.  
    49. 'Set the default return value.
    50.    RegWriteSZ = ERROR_FAILED
    51. 'Create the key.
    52.    If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, ByVal 0&, lngKeyHandle, lngRetval) = ERROR_SUCCESS Then
    53. 'Open the new key.
    54.       If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
    55. 'Write it to the registry, and return a code.
    56.          RegWriteSZ = RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue))
    57.       End If
    58.    End If
    59. 'Close any key opened with RegCreateKeyEx.
    60.    Call RegCloseKey(lngKeyHandle)
    61. End Function

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    18

    Re: Need help with writing to registry please

    thanks for the help both of you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width