Differences between CreateObject and WScript. CreateObject in VBS

Source: Internet
Author: User

This article will introduce in detail how to use CreateObject and WScript. CreateObject in VBS in vb. You can refer to it.

Many new VBS users are confused about the differences between CreateObject and WScript. CreateObject. Why do some code use CreateObject, while others use WScript. CreateObject?


Differences between CreateObject and WScript. CreateObject:
Provides the WScript object method. Similar to the relationship between internal commands and external commands in batch processing. In vbs, createobjectis always available, and wscript.createobjectcan only be used when the customer is wscript.exeand cscript.exe. It cannot be used in other host environments such as WMI, QTP, SecureCRT, and EmEditor.

Differences between CreateObject and WScript. CreateObject:
If you only use the first parameter, the CreateObject and WScript. CreateObject are almost the same. But if you want to use the second parameter, the two are completely different. The second parameter of CreateObject is used to create an object on the remote server through DCOM, while the second parameter of WScript. CreateObject is used to create a local object and respond to events.

A simple example of Event Response:

The Code is as follows: Copy code

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

Sub IE_OnQuit ()
MsgBox "disabling Demon's Blog"
WScript. Quit

The difference between End SubCreateObject and WScript. CreateObject is three:
Because CreateObject is a VBS built-in function and does not need to be called through COM, CreateObject is a little faster than WScript. CreateObject (although almost negligible ):

The Code is as follows: Copy code

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

'By Demon
'Http: // www.hzhuti.com

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

WScript. Echo Timer-tDemon suggestion: Try to use the CreateObject function unless you need to respond to the event. There are at least three advantages: Better portability, faster speed, and shorter code.

Related Article

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.