Why Can't I Create The WScript Object?

Why Can't I Create The WScript Object?

Rate This
  • Comments 59

Every now and then someone will ask me why the WSH shell object and the WSH network object are creatable from Visual Basic, but the actual root WScript object is not.

 

I am always completely mystified by why people ask this!  Why would you WANT to create the WScript object in a VB app?  What would it do for you?  It isn't creatable as an in-process object because it represents the state of a WSH process, and a VB app is not a WSH process!  The whole point of the WScript object is that it represents everything about the presently running Windows Script Host.  You can get from it all kinds of properties: the name and version of the script host, the path of the running script, the arguments which were passed in, the standard streams of the process, and whether the script was set to time out or not.  None of these properties make the slightest bit of sense to access outside of a running instance of WSH!  What the heck could this possibly mean?

 

Set WScript = CreateObject("WSH.WScript")

Timeout = WScript.TimeOut

 

The timeout of what?  You've just got some object there, not a running instance of the script host. 

 

What about the methods on the object?  Again, all of them are deeply tied into the underlying script hosting mechanisms.  WScript.CreateObject, GetObject, ConnectObject and DisconnectObject manipulate WSH-owned event sinks on the script dispatch.  I discussed yesterday how deeply tied WScript.Sleep is to the underlying hosting mechanism, and WScript.Echo is not particularly interesting -- there are better ways to create a message box in VB already.

 

I just don't get it.  Why do people keep asking me to make the WScript object creatable?  I can't figure out what they think they're going to do with it once they've created it and no one ever gives me a straight answer when I askIt's like asking for the ability to create the Response object without starting ASP!  What could it possibly mean to have a Response object but no web server process?  It doesn't make any sense, so we don't let you do it.

 

We do provide an object model whereby you can spawn off WSH processes (both locally and remotely) and query their status as they run.  Perhaps that is what these people are looking for?  Remote WSH is a complicated topic in of itself, so perhaps I'll talk about that in a later blog entry.

  • I have not proceeded far enough to know exactly what I want, but I will eventually need some mechanism in a VB component that takes some JScript or VBScript and executes it, then returns the result. Is there a some script object that will allow that?
  • Yes, the Microsoft Script Control is probably what you want. It provides a simplified interface to the script engines which you can use from VB. Here's a magazine article, or do a google search to find the documentation. http://www.microsoft.com/mind/0799/script/script.asp
  • If I create a COM object using .wsc file, I can create the WScript.Shell object, but what if I would like to write to std out and read from std in (using base WScript object), from within my COM object written in JScript (it IS running in an instance of WSH isnt it?
  • No, WSCs do not run an instance of WSH. They are just COM objects like any other. Lots of people are confused by this. I'll do a blog entry.
  • A couple of points here about the real world "question behind the question" on this that seems to crop up frequently. Here's the biggies I see in the newsgroups, and a couple might even be bloggable. :)
    + Misunderstanding of what WScript means. The progids for WshShell and WshNetwork indicate to people who are still weak on object references that they're not allowed to use them outside of WSH - the same for the Dictionary and FSO to a certain extent. This is a big thing, because frankly all of those get used out the wazoo by coders since they are so handy. WshShell is hitting it big again since the Exec method was implemented as well..
    (2) The ubiquitous use of WScript.CreateObject in the help docs. OK, it's a nice method with nice built-in event hookup, but using it for Scripting.FileSystemObject tends to catch cut'n'paste ASP programmers by surprise.
    (3) The Grass is Greener.
    A classic example of a truly useful tool is WSH's argument parsing. Sure it's got limitations, but it's THERE. It's also usage-centered. There's no robust argument parser available to people wanting to start with a standard COM object from Microsoft.
  • The reason someone would want to create a WScript object in VB, is the reason I currently want to.
    I am using the MS Script Control to run a VB script file.
    The script file has WScript references in it.
    The MS Script Control throws an error "DescriptionObject required: 'Wscript'" when it comes across a such a reference.
    The MS Script Control doesn't know what Wscript is.
    It would be handy to create a Wscript object in VB and pass it to MS Script Control, so it could indentify it and run the script as intended.

    Am I missing something here?
    How do you run a VBS script from VB, when the script has a Wscript reference in it???
  • My question then would be "why are you doing that?" If you run a WSH script without WSH, it'll fail. The same way that if you run an IE script without IE or an ASP script without ASP, it'll fail.

    Why do you expect that you should be able to run a script that uses an object model and runtime environment from a program that lacks that OM and environment?
  • Why am I doing what?
    Trying to run a VB script from a VB app?
    Because it is a requirement of our system.

    I understand the problem at hand, I am just suggesting a reason why people may ask the question frequently.

    You would think in the wonderful world of COM there would be a simple way for VB to run a VBScript file.

    The incompatibility seems absurd.

    Please let me know if you have any insight on how to run a .VBS, from a VB app (or dll), where the .VBS file contains WScript functionality.

    Thank you.
  • I have tried very much to run a remote WSH script using the WSHController object, but I got the error message "ActiveX cannot create the object" when I tried the script from Windows 2000 server to Windows Me OS and I got the error "Permission denied " when I tried the script from Windows 2000 server to Windows professional OS.
    Please tell me what I can do to resolve this problem. I need to run my scripts from my server to the clients.
  • It would be usefull to have the scripting object in VB because this would permit to code program with intellisens, object viewer, compiler, syntax analysis....
  • Here is the naswer to your question "Why would you WANT to create the WScript object in a " .... " app?"

    Because I want to to run user-defined .VBS screept but not to run them as separate process.

    There is no way you can do the Sleep if you cannot access WScript.

    The problem is the deficiencies in implementation of WScript and WSH where some important function are implemented on WScript level instead of the VBS level. You guys created the problem in the first place and then you keep asking "Why would you WANT" questions.
  • I think I see what you're saying. You want to create an object that represents a script host IN PROCESS and run scripts, right?

    We wrote that object -- it's the Microsoft Script Control. You could probably write up your own script host in VB6 that runs multiple scripts in one process and has a "Sleep" method in about 100 lines of VB, or less.
  • I am trying to run a similar code for MSI installed for a windows application in .net:

    Set oShell = WScript.CreateObject ("WScript.shell")
    oShell.run "regasm TestHarnessAPI.dll /tlb:TestHarnessAPI.tlb", 8, True

    This code registers the .net assembly as a type library. Is there any other way I can do that?
  • Just use regular old "CreateObject". Again, this is a case where the method only makes sense in WSH, and therefore we don't let you use it outside of WSH. (WScript.CreateObject participates in the WSH event binding model.)
  • I would like to use a sleep method in a script executed within a WMI consumer (the standard consumer ActiveScriptEventConsumer). You cannot use the Wscript object in those scripts either, and I would REALLY like something to pause execution. Why in hell did they not put the sleep method in WshShell or something????!!!!??? I really don't have any other option besides using an external program (.exe), what I do not whish because it add another layer of 'things that can go wrong'. So right now I'm trying to instantiate the WScript object inside the script, or trying to find something that waits for a trigger of some kind. Anybody any idea?
Page 1 of 4 (59 items) 1234