教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Vb.net  >  正文 VBS中CreateObject和WScript.CreateObject的区别

VBS中CreateObject和WScript.CreateObject的区别

发布时间:2017-12-11   编辑:jiaochengji.com
教程集为您提供VBS中CreateObject和WScript.CreateObject的区别等资源,欢迎您收藏本站,我们将为您提供最新的VBS中CreateObject和WScript.CreateObject的区别资源
本文章来给大家详细介绍关于在vb中VBS中CreateObject和WScript.CreateObject的区别用法吧,各位同学可进入参考。

多VBS新手都很困惑CreateObject和WScript.CreateObject有什么区别,为什么一些代码中用CreateObject,而另一些却用WScript.CreateObject?


CreateObject和WScript.CreateObject的区别一:
CreateObject是VBS的内置函数,属于语言的一部分;而WScript.CreateObject是Windows脚本宿主(wscript.exe和cscript.exe)提供的WScript对象的方法。类似于批处理中内部命令和外部命令的关系。在VBS中,CreateObject总是可用的,而WScript.CreateObject只有在宿主为wscript.exe和cscript.exe的情况下才可以使用,在WMI、QTP、SecureCRT、EmEditor等其他宿主环境是无法使用的。

CreateObject和WScript.CreateObject的区别二:
如果只用第一个参数,那么CreateObject和WScript.CreateObject几乎是相同的;但是如果要用到第二个参数,那么两者就完全不同了。CreateObject的第二个参数用来通过DCOM在远程服务器上创建对象;而WScript.CreateObject的第二个参数用来创建本地对象并响应事件。

一个响应事件的简单例子:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2097')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2097>

Dim IE
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate http://www.jiaochengji.com
Do
    WScript.Sleep 1000
Loop

Sub IE_OnQuit()
    MsgBox "正在关闭Demon's Blog"
    WScript.Quit

End SubCreateObject和WScript.CreateObject的区别三:
因为CreateObject是VBS的内置函数,不需要通过COM来调用,所以CreateObject比WScript.CreateObject要稍微快一点点(虽然几乎可以忽略不计):

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9892')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9892>

t = Timer
For i = 1 To 100000
    Set WshShell = WScript.CreateObject("Wscript.Shell")
Next
WScript.Echo Timer - t

'By Demon
'http://www.jiaochengji.com

t = Timer
For i = 1 To 100000
    Set WshShell = CreateObject("Wscript.Shell")
Next

WScript.Echo Timer - tDemon的建议:尽量使用CreateObject函数,除非你需要响应事件。这样至少有三个好处:可移植性更好、速度更快、代码更短。

您可能感兴趣的文章:
VBS中CreateObject和WScript.CreateObject的三个区别
VBS中CreateObject和WScript.CreateObject的区别
vbs恶作剧(整人代码)-关机 我是猪 重启 喊我爹等
访问外部文本文件的vbs方法及例子
隐藏运行bat文件的方法
vbs恶作剧(整人代码)-删除桌面 关机密码 关机前打开多个窗口等
vbs 查找硬盘分区中指定扩展名的文件
批处理隐藏运行窗口的方法
vbscript Registry 读写与删除注册表的代码
vbs 音乐播放实现代码总结

[关闭]
~ ~