Function inside a Sub (VBScript)
Fairly newish to VBScripting. I keep getting a syntax error when I run this code below from within a sub routine.
The error that I receive refers this line of code:
Am I able to call functions from within a subroutine in vbscripting? That is the only assumption that I can make. This code works PERFECTLY on it's own, outside of a sub.
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const SECS_TO_WAIT = 20
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8
Dim objWSHNetwork : Set objWSHNetwork = WScript.CreateObject("WScript.Network")
Dim oLocator : Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
If KeyExists("HKLM\Software\PROGRAMNAME\") Then
'Registry Provider (StdRegProv) lives in root\default namespace.
Dim wmiNameSpace : Set wmiNameSpace = oLocator.ConnectServer(objWSHNetwork.ComputerName, "root\default")
Dim objRegistry : Set objRegistry = wmiNameSpace.Get("StdRegProv")
Dim sNames, sKeyName, sEnumPath, lRC
' Example Deletion of Value
Dim sPath : sPath = "SOFTWARE\PROGRAMNAME"
lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)
Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key. If it fails, start the subkey enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then
' Subkey Enumerator
On Error Resume Next
lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
Next
On Error Goto 0
'At this point we should have looped through all subkeys, trying to delete the registry key again.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
End If
End Function
End If
Function KeyExists(key)
On Error Resume Next
objWSHSHell.RegRead (key)
If Err = 0 Then KeyExists = True
End Function
The error that I receive refers this line of code:
Function DeleteRegEntry(sHive, sEnumPath)
Am I able to call functions from within a subroutine in vbscripting? That is the only assumption that I can make. This code works PERFECTLY on it's own, outside of a sub.
0 Comments
[ + ] Show comments
Answers (4)
Please log in to answer
Posted by:
murali.bhat
13 years ago
Posted by:
pjgeutjens
13 years ago
Posted by:
mrwillya
13 years ago
ORIGINAL: murali.bhat
I tested your script by creating a dummy key PROGRAMNAME. I had to make small change to function KeyExists as object objWSHSHell wasnt created. The script worked without error.
Oops, I forgot to copy that over from the main script. This is just a section of the main script. objWSHSHell was defined elsewhere.
I just tested the script alone too and it worked fine, but when added as a sub to my main script, I receive the error. So it works fine stand alone, but when it's made into a sub routine in my main script, it errors out. WTH?
ORIGINAL: pjgeutjens
Why are you defining the function DeleteRegEntry inside an If loop?
this strikes me as very odd, just lift the function outside the loop.
       End If
  End Function
End If
PJ
This is a snipet used from MS, only things I modified were to match what my varibles were named.
Posted by:
pjgeutjens
13 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.