What is Binding
Before we dive into ‘Early’ and ‘Late’ stuff, let us discover, what the ‘Binding’ itself means?
In simple terms, the ‘Binding’ refers to the way a script or an application accesses a COM Automation object. This is manifested as the association of entities such as data, program, procedures etc. with identifiers.
It is done when the application is compiled and all functions called in code must be bound before the code can be executed. In the premise of VBScripting, when a script or application attempts to create an instance of a COM object, it inherently verifies the existence of the object in the relevant or specified namespace besides this the object’s methods and properties (called in the script) are also verified. This entire process of locating and verification while creating an instance of COM Object is called ‘Binding’.
Binding with VBScript
Here are some typical examples of binding –
Set MyObject = CreateObject ("Word.Application") Set MyObject = GetObject ("winmgmts:") Set MyObject = WScript.CreateObject ("Scripting.FileSystemObject")
In general, there are two methods to bind to a COM object using VBScript syntax as specified below (other variations exist as well depending upon the specific needs or style) –
- CreateObject (“[Component or Server or Moniker][.][Object or typename]”)
- GetObject (“[Component or Server or Moniker][.][Object or typename]”)
However, if you wish to use purely WSH scripts, then the syntax would slightly change to accommodate the prefix – ‘WScript’ and as in ‘WScript.CreateObject’ and ‘WScript.GetObject’.
What is the difference between the above two methods, i.e. CreateObject and GetObject again requires a good explanation which may be out of scope of this post. But in short – ‘You should use GetObject while using monikers and if the object to which you are binding exists outside the ‘File System’ namespace examples would be WMI and ADSI. For all other purposes CreateObject should do fine
Coming back to Early and Late terms, the ‘COM’ support two types of binding –
1Early Binding :With this type of binding the ‘COM Object’, its methods and properties are all checked when the script or program is compiled. This has some obvious advantages. Firstly any problems with ‘COM Object’ binding if exist makes the compilation to fail and hence results in the early discovery of related issues. Secondly, this provides the advantage of early access to object model and library (COM) and hence the related information is loaded itself into the compilation or the program. Lastly, as obvious scripts or programs supporting ‘Early Bindinding’ are relatively faster.
2Late Binding : Since VBScript is a not a compiled language, natively it supports only ‘Late Binding’. Hence unlike the ‘Early Binding’, in case of VBScript the binding does not occur until the program is actually executed. In this scenario the program usually performs frequent registry or namespace lookups upon execution to access the information about the ‘COM Objects’, consequently any issues with the COM information are not discovered until the actual execution of the script/program.