Does anyone know the reasoning behind having the option using:
有没有人知道使用该选项背后的原因:
Wscript.CreateObject("some.object")
and
CreateObject("some.object")
within VBScript? when I find documentation or examples that use Wscript.CreateObject
, I usually rewrite using CreateObject
, because it always seems to work, and then I can easily reuse the code within an HTA or ASP. But I've always wondered why this feature existed and if what difference it makes if you use one way or another within VBScript.
在VBScript中?当我找到使用Wscript.CreateObject的文档或示例时,我通常使用CreateObject重写,因为它似乎总是有效,然后我可以轻松地在HTA或ASP中重用代码。但我一直想知道为什么这个功能存在,如果你在VBScript中使用这种或那种方式会有什么不同。
3 个解决方案
#1
There's no difference between the two, when you call them with just one argument. The do exactly the same thing.
当你用一个参数调用它们时,两者之间没有区别。做完全一样的事情。
The difference between the two is only in evidence if you call with two parameters. The statements
如果您使用两个参数调用,则两者之间的差异仅在证据中。声明
Wscript.CreateObject("some.object", "AnotherParam")
and
CreateObject("some.object", "AnotherParam")
do completely different things:
做完全不同的事情:
The VBScript CreateObject function interprets the second parameter as a remote computer name and tries to create the named COM object on that remote computer; in this example, it tries to instantiate an instance of an object with ProgId of "some.object" on a remote computer named "AnotherParam". The WScript CreateObject method interprets the second parameter as a subroutine prefix to be used in handling events from the object. The two GetObject functions are similarly related.
VBScript CreateObject函数将第二个参数解释为远程计算机名称,并尝试在该远程计算机上创建命名的COM对象;在此示例中,它尝试在名为“AnotherParam”的远程计算机上使用ProgId“some.object”实例化对象的实例。 WScript CreateObject方法将第二个参数解释为子例程前缀,用于处理来自对象的事件。两个GetObject函数类似地相关。
(Adapted from TechNet, section "Comparing VBScript CreateObject and GetObject Functions with WSH".)
(改编自TechNet,“将VBScript CreateObject和GetObject函数与WSH进行比较”一节。)
#2
I guess that the WScript
object has the CreateObject
method so any Windows Script language can create COM objects.
我猜WScript对象具有CreateObject方法,因此任何Windows脚本语言都可以创建COM对象。
VBScript has that ability as a global function, but other Windows Script host languages might not.
VBScript具有作为全局函数的能力,但其他Windows Script主机语言可能不具备。
For instance, JScript does not have a global CreateObject
function (I believe) (it does, however, have a var a = new ActiveXObject("...")
syntax, so you do not need to use WScript.CreateObject
in JScript either).
例如,JScript没有全局的CreateObject函数(我相信)(但它有一个var a = new ActiveXObject(“...”)语法,所以你不需要在JScript中使用WScript.CreateObject )。
I would guess there is no difference between the two functions.
我猜这两个函数没有区别。
EDIT: There is a difference (but only if you're trying to instantiate DCOM objects on remote hosts), see the answer by @Thomas Petersen.
编辑:有区别(但只有当您尝试在远程主机上实例化DCOM对象时),请参阅@Thomas Petersen的答案。
#3
JScript does not have a global CreateObject ? WScript can't use JScript ?
JScript没有全局CreateObject? WScript不能使用JScript?
来自devGuru的代码
// JScript
var objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true
while (objIE.Visible){
WScript.Sleep(500);
}
function objIE_NavigateComplete2(pDisp, URL){
WScript.Echo("You just navigated to", URL)
}
function objIE_OnQuit(){
boolBrowserRunning = false ;
}
#1
There's no difference between the two, when you call them with just one argument. The do exactly the same thing.
当你用一个参数调用它们时,两者之间没有区别。做完全一样的事情。
The difference between the two is only in evidence if you call with two parameters. The statements
如果您使用两个参数调用,则两者之间的差异仅在证据中。声明
Wscript.CreateObject("some.object", "AnotherParam")
and
CreateObject("some.object", "AnotherParam")
do completely different things:
做完全不同的事情:
The VBScript CreateObject function interprets the second parameter as a remote computer name and tries to create the named COM object on that remote computer; in this example, it tries to instantiate an instance of an object with ProgId of "some.object" on a remote computer named "AnotherParam". The WScript CreateObject method interprets the second parameter as a subroutine prefix to be used in handling events from the object. The two GetObject functions are similarly related.
VBScript CreateObject函数将第二个参数解释为远程计算机名称,并尝试在该远程计算机上创建命名的COM对象;在此示例中,它尝试在名为“AnotherParam”的远程计算机上使用ProgId“some.object”实例化对象的实例。 WScript CreateObject方法将第二个参数解释为子例程前缀,用于处理来自对象的事件。两个GetObject函数类似地相关。
(Adapted from TechNet, section "Comparing VBScript CreateObject and GetObject Functions with WSH".)
(改编自TechNet,“将VBScript CreateObject和GetObject函数与WSH进行比较”一节。)
#2
I guess that the WScript
object has the CreateObject
method so any Windows Script language can create COM objects.
我猜WScript对象具有CreateObject方法,因此任何Windows脚本语言都可以创建COM对象。
VBScript has that ability as a global function, but other Windows Script host languages might not.
VBScript具有作为全局函数的能力,但其他Windows Script主机语言可能不具备。
For instance, JScript does not have a global CreateObject
function (I believe) (it does, however, have a var a = new ActiveXObject("...")
syntax, so you do not need to use WScript.CreateObject
in JScript either).
例如,JScript没有全局的CreateObject函数(我相信)(但它有一个var a = new ActiveXObject(“...”)语法,所以你不需要在JScript中使用WScript.CreateObject )。
I would guess there is no difference between the two functions.
我猜这两个函数没有区别。
EDIT: There is a difference (but only if you're trying to instantiate DCOM objects on remote hosts), see the answer by @Thomas Petersen.
编辑:有区别(但只有当您尝试在远程主机上实例化DCOM对象时),请参阅@Thomas Petersen的答案。
#3
JScript does not have a global CreateObject ? WScript can't use JScript ?
JScript没有全局CreateObject? WScript不能使用JScript?
来自devGuru的代码
// JScript
var objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true
while (objIE.Visible){
WScript.Sleep(500);
}
function objIE_NavigateComplete2(pDisp, URL){
WScript.Echo("You just navigated to", URL)
}
function objIE_OnQuit(){
boolBrowserRunning = false ;
}