windows CreateObject 和 Wscript.CreateObject 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/787369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between CreateObject and Wscript.CreateObject?
提问by mrTomahawk
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 中使用一种或另一种方式,它会产生什么不同。
回答by Thomas Petersen
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”部分。)
回答by codeape
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 脚本宿主语言可能没有。
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("...")
语法,因此您也不需要WScript.CreateObject
在 JScript 中使用)。
I would guess there is no difference between the two functions.
我猜这两个功能之间没有区别。
EDIT: There isa difference (but only if you're trying to instantiate DCOM objects on remote hosts), see the answer by @Thomas Petersen.
编辑:有是一个差异(但只有当你试图实例DCOM远程主机上的对象),看到@Thomas彼得森答案。
回答by index
JScript does not have a global CreateObject ? WScript can't use JScript ?
JScript 没有全局 CreateObject 吗?WScript 不能使用 JScript 吗?
// 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 ;
}