|
-
Dec 18th, 2008, 02:46 AM
#1
Thread Starter
Junior Member
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.
-
Dec 18th, 2008, 04:42 AM
#2
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.
-
Dec 18th, 2008, 05:15 AM
#3
Thread Starter
Junior Member
Re: Need help with writing to registry please
im unfamiliar, do you have any links please
-
Dec 18th, 2008, 05:55 AM
#4
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:
Const WINMGMTS = "winmgmts:{impersonationLevel=impersonate}!\\"
Const STDREGPROV = "\root\default:StdRegProv"
Const strComputer = "."
Const ERROR_FAILED = -999&
Const ERROR_SUCCESS = 0&
Public Function WmiWriteSZ(strComputer As String, lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
'Create a key and respective REG_SZ value from a simple string
'eg:- "Hello World".
Dim objReg As Object
'Set the default return value.
WmiWriteSZ = ERROR_FAILED
'Create the WMI StdRegProv object.
Set objReg = GetObject(WINMGMTS & strComputer & STDREGPROV)
'Create the subkey.
If objReg.CreateKey(lngHKey, strSubKey) = ERROR_SUCCESS Then
'Write it to the registry, and return a code.
WmiWriteSZ = objReg.SetStringValue(lngHKey, strSubKey, strValueName, strValue)
End If
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.
-
Dec 18th, 2008, 07:02 PM
#5
Thread Starter
Junior Member
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:
Const WINMGMTS = "winmgmts:{impersonationLevel=impersonate}!\\"
Const STDREGPROV = "\root\default:StdRegProv"
Const strComputer = "."
Const ERROR_FAILED = -999&
Const ERROR_SUCCESS = 0&
Public Function WmiWriteSZ(strComputer As String, lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
lngHKey = "HKEY_LOCAL_MACHINE"
strSubKey = "\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List\"
strValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
strValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
'Create a key and respective REG_SZ value from a simple string
'eg:- "Hello World".
Dim objReg As Object
'Set the default return value.
WmiWriteSZ = ERROR_FAILED
'Create the WMI StdRegProv object.
Set objReg = GetObject(WINMGMTS & strComputer & STDREGPROV)
'Create the subkey.
If objReg.CreateKey(lngHKey, strSubKey) = ERROR_SUCCESS Then
'Write it to the registry, and return a code.
WmiWriteSZ = objReg.SetStringValue(lngHKey, strSubKey, strValueName, strValue)
End If
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
-
Dec 18th, 2008, 07:21 PM
#6
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.
-
Dec 18th, 2008, 08:15 PM
#7
Thread Starter
Junior Member
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?
-
Dec 19th, 2008, 05:12 AM
#8
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:
Const HKEY_LOCAL_MACHINE = &H80000002 '<<<--------
Const MySubKey = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List"
Const MyValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
Const MyValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
Private Sub Form_Click()
MsgBox WmiWriteSZ(strComputer, HKEY_LOCAL_MACHINE, MySubKey, MyValueName, MyValue)
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:
Option Explicit
'Creates the specified registry key if necessary.
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal Hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
'Opens the specified registry key.
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
'Sets the data and type of a specified value under a registry key.
Private 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.
'Closes a handle to the specified registry key.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
'Error handling.
Private Const ERROR_FAILED = -999& 'Custom.
Private Const ERROR_SUCCESS = 0&
'Registry manipulation
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_SET_VALUE = &H2
Private Const REG_SZ = 1
'Other stuff.
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const MySubKey = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List"
Private Const MyValueName = "C:\Test\DoesThis\CreateNew\Folders.exe"
Private Const MyValue = "C:\Test\DoesThis\CreateNew\Folders.exe:*:Enabled:Folders"
Private Sub Form_Click()
MsgBox RegWriteSZ(HKEY_LOCAL_MACHINE, MySubKey, MyValueName, MyValue)
End Sub
Private Function RegWriteSZ(lngHKey As Long, strSubKey As String, strValueName As String, strValue As String) As Long
'Create a key and respective REG_SZ value from a simple string
'eg:- "Hello World".
Dim lngRetval As Long
Dim lngKeyHandle As Long
'Set the default return value.
RegWriteSZ = ERROR_FAILED
'Create the key.
If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, ByVal 0&, lngKeyHandle, lngRetval) = ERROR_SUCCESS Then
'Open the new key.
If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) = ERROR_SUCCESS Then
'Write it to the registry, and return a code.
RegWriteSZ = RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue))
End If
End If
'Close any key opened with RegCreateKeyEx.
Call RegCloseKey(lngKeyHandle)
End Function
-
Dec 19th, 2008, 07:21 AM
#9
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|