vbs syntax error: 800A03EE ')' expected

2020-02-07 09:49发布

I try to execute this in a file called 'addcurrentkey.vbs' But it says ')' is expected in row 1. Character 38.

I tried this tutorial: http://www.codeproject.com/Articles/16569/Autorun-Applications

Why can't I execute a .vbs file?

Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True) 
key.SetValue(name, path) 
End Sub 

2条回答
够拽才男人
2楼-- · 2020-02-07 10:18

The code you posted is probably written in VB.net (or perhaps VBA). You are tying to run the code as VBScript, which does not support typed parameters and variables. It also doesn't provide the registry object you're trying to use. Change the procedure from this:

Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
  Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True) 
  key.SetValue(name, path) 
End Sub

to this:

Private Sub AddCurrentKey(ByVal name, ByVal path)
  Dim key : key = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
  CreateObject("WScript.Shell").RegWrite key & "\" & name, path
End Sub

and the problem will disappear.

查看更多
再贱就再见
3楼-- · 2020-02-07 10:38

VBScript still uses the "old" Visual Basic syntax. Which distinguishes between function calls used in expressions and procedure calls that are statements. You use (parentheses) in an expression but not in a statement. Fix:

 key.SetValue name, path 

Or if you prefer:

 Call key.SetValue(name, path)

The error message is hard to interpret because the script interpreter thinks that you are trying to write this:

 key.SetValue (name), path

Which means something completely different. VBScript passes arguments ByRef. The extra parentheses around name turns it into an expression that creates a copy of the variable. It can be modified by the SetValue procedure without it affecting the name variable. Not what it actually does nor what you intended.

查看更多
登录 后发表回答