Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between CreateObject and Wscript.CreateObject?

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.

like image 767
mrTomahawk Avatar asked Apr 24 '09 20:04

mrTomahawk


People also ask

What is Wscript CreateObject?

CreateObject Method The CreateObject method creates a new instance of a specified COM object. Once the object has been successfully created, you can access any properties or call any methods the object exposes. Its syntax is: Set objObject = Wscript.CreateObject( strObjectId )

What is the use of wscript shell?

If you do not specify a script or any script arguments, wscript.exe displays the Windows Script Host Settings dialog box, which you can use to set global scripting properties for all scripts that wscript.exe runs on the local computer. The /t parameter prevents excessive running of scripts by setting a timer.


3 Answers

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.

(Adapted from TechNet, section "Comparing VBScript CreateObject and GetObject Functions with WSH".)

like image 88
Thomas Petersen Avatar answered Oct 06 '22 00:10

Thomas Petersen


I guess that the WScript object has the CreateObject method so any Windows Script language can create COM objects.

VBScript has that ability as a global function, but other Windows Script host languages might not.

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).

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.

like image 21
codeape Avatar answered Oct 05 '22 23:10

codeape


JScript does not have a global CreateObject ? WScript can't use JScript ?

Code from 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 ;
}
like image 24
index Avatar answered Oct 06 '22 00:10

index