What is the difference between CreateObject and Wscript.CreateObject?

前端 未结 3 648
梦谈多话
梦谈多话 2020-12-09 10:32

Does anyone know the reasoning behind having the option using:

Wscript.CreateObject(\"some.object\")

and

CreateObject(\"som         


        
相关标签:
3条回答
  • 2020-12-09 11:09

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

    0 讨论(0)
  • 2020-12-09 11:14

    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.

    0 讨论(0)
  • 2020-12-09 11:16

    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 ;
    }
    
    0 讨论(0)
提交回复
热议问题