“Object required” when using Set in an assignment

后端 未结 3 679
耶瑟儿~
耶瑟儿~ 2020-12-06 16:49
call main()
sub main()
    Dim scmd
    Set scmd = \"c:\\windows\\system32\\cscript.exe //nologo c:\\s.vbs\"
    createobject(\"wscript.shell\").run scmd,0,false
end         


        
相关标签:
3条回答
  • 2020-12-06 17:21

    I am not sure, try change

    Set scmd = "c:\windows\system32\cscript.exe //nologo c:\s.vbs"
    

    to

    Set scmd = "c:\windows\system32\cscript.exe" //nologo "c:\s.vbs"
    
    0 讨论(0)
  • 2020-12-06 17:22

    As

    >> WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
    >>
    E:\trials\SoTrials\answers\trials\AlgoTaBu\SuSo\wsf
    

    proves, there is no rule or law at all that "Your Set scmd should be instantiating the WScript.Shell". Putting the command to execute in string variable scmd (or perhaps better sCmd) and not creating a variable for an only-once-used value are good practices.

    The revised version (minus the stupid Set):

    call main()
    sub main()
        Dim scmd
        scmd = "c:\windows\system32\cscript.exe //nologo c:\s.vbs"
        createobject("wscript.shell").run scmd,0,false
    end sub
    

    will work just as well as Lankymart's version.

    To spell everything out:

    1. The Set keyword, its semantic, and its error message are design flaws, that make VBScript harder to use correctly. "site:stackoverflow.com vbscript "object required" Set" results in 1500 hits. Even if much of those hits do not concern the "Set x = 'non-object' blunder, that's clearly too much. To explain/excuse those IEDs you have to consider that BASIC is a stone age language.
    2. A person learning VBScript is entitled to be surprised by the "Set x = 'non-object' mistake twice. If it happens trice (or more often), he/she should be ashamed (and keep silent about it). Above all that problem should not pollute this site.
    3. When I posted my contribution, all answers/comments - with the exception of Alex K.'s 'Just delete the Set' - emphasized/concentrated on the .Run statement; one answer called the script "topsy curvy", one answer even repeated the atrocity. So I tried to point out that there is exactly one error: The spurious Set.
    4. I failed miserably. Evidence: John Saunders changed the title from "VBScript error" (unspecific but true) to "“Object required” when calling Run on Wscript.Shell" (specific but wrong), Lankymart engaged in psychological/philological research to save the Set at the expense of the string.
    5. My only hope: Everybody reading this will be so disgusted by my harping on the Set, that she/he from now on will think twice when typing:

    wtf

    Set x = "     ---- stop or be damned!!!
    Set x = obj.getNumber() + 4  ---- oh no!!!
    
    1. All to no avail - Same mistake again
    0 讨论(0)
  • 2020-12-06 17:28

    Update

    As it's not clear feel it best to point out your Object Required issue is due to this line

    Set scmd = "c:\windows\system32\cscript.exe //nologo c:\s.vbs"
    

    This is because an Object is expected but you are assigning it a string, by removing the Set your code will work (As Ekkehard.Horner has pointed out).


    Below is my interpretation of situation. First looking at your code it almost looked like it had mixed the instantiation of the WScript.Shell object with the command line for the .Run() method. It was my first stab at breaking down the code, rearranging it then putting it back together.


    Original Answer

    1. Your Set scmd should be instantiating the WScript.Shell (As Ekkehard.Horner points out you can use Server.CreateObject("WScript.Shell").Run for a one off reference but I wouldn't recommend it).

    2. The .Run() should be executed by the instantiated scmd object and passed the command line to execute.

    Here is an example I've renamed some of the variables (scmd to cmd for example).

    Call main()
    
    Sub main()
        'Renamed variables to cmd is your object and cmdline is your file path.
        Dim cmd, cmdline
        'Instantiate WshShell object
        Set cmd = Server.Createobject("WScript.Shell")
        'Set cmdline variable to file path
        cmdline = "c:\windows\system32\cscript.exe //nologo c:\s.vbs"
        'Execute Run and return immediately
        Call cmd.Run(cmdline, 0, False)
    End Sub
    

    Things to consider

    When using WScript.Shell in Classic ASP to run executables there are some things to consider;

    1. Run command will execute using the current Application Pool identity.

    2. Run will execute the executable on the server not at the client (server side).

    0 讨论(0)
提交回复
热议问题