How getObject Function internally works?

前端 未结 2 483
悲哀的现实
悲哀的现实 2020-12-17 02:41

I\'m Automating Inventor 2013 using UFT as follows:-

Set oApp = GetObject(,\"Inventor.Application\") Set oDoc = oApp.ActiveDocument

Here I

相关标签:
2条回答
  • 2020-12-17 03:28

    GetObject and CreateObject are part of COM automation provided by VBScript. VBScript can't use all the COM objects available through Windows. VBScript can use only those objects that expose a string called a programmatic identifier (ProgID). Although not all COM objects have a ProgID, all COM objects have a 128-bit number called a class identifier, or CLSID. If a COM object has a ProgID, you can use VBScript to instantiate the object, invoke its methods and properties, and destroy the object.

    GetObject and CreateObject works similar in a way, but they serve different purposes.
    If you need to create a new instance of an object, use CreateObject.
    If you need to reference an existing instance of an object, use GetObject.

    GetObject function has two optional arguments: the object's pathname (i.e., a full path and a filename) and the object's ProgID. Although both arguments are optional, you must specify at least one. If you omit both arguments, an error results. For example:

    Dim wordDoc
    Set wordDoc = GetObject ("FilePath\FileName.doc")
    

    When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated. If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

    If you specify the ProgID but not the pathname, the results differ depending on how you set up the arguments. If you pass an empty string as the first argument in code such as

    Set wordApp = GetObject("", "Word.Application")
    

    VBScript returns a new instance of Word's Application object (i.e., an object that represents the Word application). This GetObject call is equivalent to the CreateObject call

    Set wordApp = CreateObject ("Word.Application")
    

    If you omit the pathname argument but leave the comma

    Set wordApp = GetObject (, "Word.Application")
    

    VBScript returns an existing instance of the Application object if one exists.

    For more information, check this and this links.

    0 讨论(0)
  • 2020-12-17 03:32

    You turn off error checking, attempt a GetObject and then test err.number <> 0. If inventor is running the call will succeed and err.number will be 0, else it will be 424 error (I think).

    The API calls made for each variant of the GetObject are detailed at https://msdn.microsoft.com/en-us/library/windows/desktop/ms221192(v=vs.85).aspx.

    From above link.

    GetObject (filename, ProgID)

    CLSIDFromProgID 
    
    CoCreateInstance 
    
    QueryInterface for IPersistFile interface.
    
    Load on IPersistFile interface.
    
    QueryInterface to get IDispatch interface. 
    

    GetObject (filename)

    CreateBindCtx creates the bind context for the subsequent functions.
    
    MkParseDisplayName returns a moniker handle for BindMoniker.
    
    BindMoniker returns a pointer to the IDispatch interface.
    
    Release on moniker handle.
    
    Release on context.
    

    GetObject (ProgID)

    CLSIDFromProgID 
    
    GetActiveObject on class ID.
    
    QueryInterface to get IDispatch interface. 
    

    You can look up each individual function call here

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms680586(v=vs.85).aspx

    EG

    CLSIDFromProgID function

    Looks up a CLSID in the registry, given a ProgID.

    Syntax

    HRESULT CLSIDFromProgID(
      _In_  LPCOLESTR lpszProgID,
      _Out_ LPCLSID   lpclsid
    );
    
    0 讨论(0)
提交回复
热议问题