Introduction to the difference between CreateObject and WScript.CreateObject in VBS _VBS

Source: Internet
Author: User
The difference between CreateObject and WScript.CreateObject one:
CreateObject is a built-in function of VBS, part of the language, and WScript.CreateObject is a method of WScript objects provided by Windows Script hosts (Wscript.exe and Cscript.exe). Similar to the relationship between internal commands and external commands in a batch. In the VBS, CreateObject is always available, and wscript.createobject can be used only if the host is Wscript.exe and Cscript.exe, in WMI, QTP, SECURECRT, Other hosting environments, such as EmEditor, are not available.

the difference between CreateObject and WScript.CreateObject two:
If you use only the first argument, then CreateObject and WScript.CreateObject are almost identical, but if you use the second argument, the two are completely different. The second parameter of CreateObject is used to create the object on the remote server through DCOM, while the second parameter of WScript.CreateObject is used to create the local object and respond to the event.

A simple example of a response to an event:
Copy Code code as follows:

Dim IE
Set IE = WScript.CreateObject ("Internetexplorer.application", "Ie_")
Ie. Visible = True
Ie. Navigate "Http://www.jb51.net"
Todo
Wscript.Sleep 1000
Loop

Sub Ie_onquit ()
MsgBox "is shutting down Demon ' s Blog"
Wscript.Quit
End Sub


the difference between CreateObject and WScript.CreateObject three:

Because CreateObject is a built-in function of VBS and does not need to be invoked through COM, CreateObject is a little bit faster than wscript.createobject (though almost negligible):

Copy Code code as follows:

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

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


Demon's advice: try to use the CreateObject function unless you need to respond to the event. There are at least three benefits: portability, faster, and shorter code.

Author: Demon
Link: http://demon.tw/programming/vbs-createobject-wscript-createobject.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.