VBScript and CreateObject issue

感情迁移 提交于 2019-11-26 18:43:42

问题


I'm not really experimented if it is about VBScript but I had the occasion to read many things through forums and actually yesterday I helped someone to checkout why his script wasn't working and found him a solution. So I modified this script locally and did it the way I would and it worked but on that other person side one of the object couldn't be initialized.

The incriminated line is like

    Set WshNet = WScript.CreateObject("WScript.Network")

Another person told to remove the WScript thing and it seems it works on the question asker side.

I first thought it might be linked with the use of Wscript.exe on my side and CScript.exe on that person side (my hypothesis) but I checked this out in the command-line and it worked anyway. So, what I would like to know and understand is why is this happening ? Why does a script calling for CreateObject works with "WScript." but on another system you need to remove that "WScript." to keep it working ? Thank you for your time and answers. ;) Best regards.


回答1:


The "WScript" in "WScript.CreateObject" or "WScript.Echo" is (the name of an object) provided by the c|wscript.exe scripting host. If you run VBScript under/in other hosts (e.g. ie or mshta), there is no such object.

The language itself has a (different!, see the docs for details) "CreateObject" function, that can be used under/in all VBScripts hosts. So use plain "CreateObject" to be on the safe side.

The WScript in "WScript.Network" is part of the ProgId of a COM object that may be installed on your computer (or not). Those COM objects with a 'first name' of "WScript" are completely different from the WScript object provided by c|wscript.exe.

So my first assumption: The "someone" executed the code under/in ie or mshta and solved the problem by removing the "WScript." from "WScript.CreateObject", that is by falling back to VBScript's own CreateObject.



来源:https://stackoverflow.com/questions/8915052/vbscript-and-createobject-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!