<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Data types and variables > iDispatch variables |
iDispatch data type
iDispatch variables are used to interact with COM object.
iDispatch variables contain a pointer to the desired object, so they are considered to contain an "Object Reference". They contain no other value of any kind.
COM stands for "Component Object Model". It is the Microsoft way to interconnect software using a common interface. These interfaces are defined in a COM Object.
Before COM, you had to know the exact implementation of a program before you could 'interface' with it. Using COM, you can now "talk" to its defined Object(s). The only things you have to know are the names of the Objects that are used and which properties or methods they have.
What are Object properties or methods?
These are the two basic characteristics of an Object:
1.you can see a property as the data storage of an Object
2.you can see a method as an internal function call to do something with the data.
How to create an iDispatch variable
An iDispatch variable is created just like all other variables using DIM or LOCAL. Example:
'---Declare a new iDispatch variable
dim oShell as iDispatch
How to initialize an iDispatch variable with an object reference
Just creating an iDispatch variable is not that useful: it is just an empty pointer to nothing.
You need to assign an object reference to an iDispatch variable to make something with it.
To assign an object reference use one of the following command: NewCom, GetCom, AnyCom.
Example
'---Assign an object reference
oShell = NewCom("Shell.Application")
How to use an iDispatch variable
Once an iDispatch has been initialized with a new object reference, it inherits the object properties and methods.
You have to search the object documentation to have a peek of what properties and methods are implemented.
For example "Shell.Application" information can be found here: https://msdn.microsoft.com/en-us/library/bb774094.aspx
How to check if an iDispatch variable is referencing to an object
It is always convenient to check if an iDispatch variable is referencing to an object. To do so use IsComObject function.
How to release an iDispatch variable
It is always necessary to release an iDispatch variable if it is referencing to an object. To do so assign Nothing to the iDispatch variable.
This destroys an iDispatch variable, discontinuing its association with a specific object.
This in turn releases all system and memory resources associated with the object when no more object variables refer to it.
Remarks/Restrictions
1.Only objects implementing dual interfaces (Dispatch) can be handled in thinBasic.
2.complete compound dotted notation is NOT yet permitted. Only single dotted notation.
Example:
object.method is permitted
object.property is permitted
object.object.method is NOT permitted
Complete example
'---The following example creates a "Shell.Application" object
'---If all OK, all windows currently on screen are minimized, and after 2 seconds unminized
uses "Console"
dim oShell as iDispatch
printl "Creating a Shell.Application object"
oShell = NewCom("Shell.Application")
if IsComObject(oShell) Then
printl "Now minimizing all windows, waiting for 2 seconds and then UndoMinimizeALL"
oShell.MinimizeAll
sleep 2000
oShell.UndoMinimizeALL
oShell = Nothing
Else
printl "It was not possible to create Shell.Application object"
end If
printl "---Press a key to finish---"
WaitKey