Repository navigation
SysWin Com
Home › All Categories › sys/win › sys/win/com
Wraps COM for tiko: variants, safe arrays, BSTR strings and late-bound Automation calls.
COM is how Windows components and Automation servers (the scripting runtime, Office, WMI, the
shell) expose objects to any language. Their methods take and return three data types tiko does
not have natively: the VARIANT, a value of any type; the SAFEARRAY, an array that knows its
own bounds and element type; and the BSTR, a length-prefixed Unicode string. This module gives
each one a class that owns its memory and converts to and from tiko values, plus a class that
calls an object's methods and properties by name, and the functions that create objects.
The rules that apply throughout:
-
Initialise COM on each thread that uses it:
CoInitialize( NULL )first,CoUninitialize()last. CDispInvoke also does this for itself. -
The classes own what they hold. A
DVARIANT,DSafeArrayorBSTRINGfrees its contents when it goes out of scope or is assigned again; aCDispInvokereleases its object. Release every raw interface pointer you are given yourself, with SafeRelease. -
Destroy the objects before
CoUninitialize. Put the COM work in its ownscope, or a procedure, so that every wrapper is gone when COM is shut down, as the example does.
#import "sys/win"
#import "sys/win/com"
using win, comA Scripting.Dictionary, driven by name through CDispInvoke, with its keys read back from the
array of variants it returns:
#import "sys/win"
#import "sys/win/com"
using win, com
function main() as long
CoInitialize( NULL )
scope
' a Scripting.Dictionary, driven by name through IDispatch
dim pDic as CDispInvoke = "Scripting.Dictionary"
if pDic.IsObject() = false then print "Scripting.Dictionary is not available" : return 1
pDic.Invoke( "Add", "ATH", "Athens" )
pDic.Invoke( "Add", "ROM", "Rome" )
pDic.Invoke( "Add", "OSL", "Oslo" )
print "count:"; pDic.Get( "Count" ).ToStr()
print "ROM is "; pDic.Get( "Item", "ROM" ).ToStr()
' Keys returns a VARIANT holding a safe array of VARIANTs
dim keys as DVARIANT = pDic.Invoke( "Keys" )
print "keys: "; keys.ToStr()
for i as long = keys.GetLBound() to keys.GetUBound()
print i; " "; keys.GetVariantElement( i ).ToStr()
next
end scope
CoUninitialize()
return 0
end function
end main()It prints:
count:3
ROM is Rome
keys: ATH; ROM; OSL
0 ATH
1 ROM
2 OSL
| Page | Covers |
|---|---|
| DVARIANT | The VARIANT class: assigning and reading numbers, strings, dates, decimals, objects and arrays; conversions, arithmetic and comparison operators. |
| DSafeArray | The SAFEARRAY class: creating one- and two-dimensional arrays of any element type, bounds, reading and writing elements, resizing, appending, copying. |
| BSTRING | The BSTR class: a COM string that converts to and from wstring, ustring and UTF-8, for methods that take or return a BSTR. |
| CInt96 | A 96-bit integer built on DECIMAL, with arithmetic, bitwise and shift operators and conversion to text and variants. |
| CDispInvoke | Late binding: creates or wraps an Automation object and gets and sets its properties and calls its methods by name or DISPID, with DVARIANT arguments and results. |
| COM procedures |
NewCom, GetCom, AnyCom, reference counting, event connections, GUIDs, OLE fonts, VarToStr and the variant-type tests; the ComUnknown and ComDispatch base types. |
- Category: sys/win
- WebView2 and Direct2D, which are COM interfaces too
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing