Working on more complex examples for SimpleCOM.bi: http://www.freebasic.net/forum/viewtopi ... =6&t=24738
it became apparent that without a Variant-Wrapper-Type (inlcuding proper Assignment- and Cast-Methods), "there's just no fun"...
So, here is SimpleVariant.bi, which covers already most of the stuff which is needed to bring the fun back <g>,
it still has no lib- or *.a dependency - and so far remains small enough (only about 500 lines), to not make
the compiler groan whilst parsing the include-file ...
SimpleVariant.bi includes all the functionality from SimpleCOM.bi (directly "in-file"), so only this single
module is needed for comfortable Variant- and LateBound-COM-stuff (it's a full, and better replacement).
Last Update: 2016-06-22 (introduced a simple approach for fast GetIdsOfNames-Caching)
Here's the updated Demo-Zip: http://vbRichClient.com/Downloads/SimpleVariant.zip
It includes (along with SimpleVariant.bi) 3 small examples:
- 1_Hello_SimpleVariant.bas
- 2_Regfree_COM_and_ByRef_passing.bas
- 3_OutOfProcess_ExcelAutomation.bas
For the latter one (which is derived from an example of aloberoger) - here the complete Source:
Please note that, in case you copy that code into a Code-Module directly (not using the one from the Zip),
one has to make sure that this CodeModule is flagged as an Unicode-one... usually with an UTF8-BOM).
Code: Select all
#define UNICODE
#Include Once "SimpleVariant.bi"
'Whilst the other examples so far, were creating InProcess-ObjectInstances -
'the following snippet covers an OutOfProcess-COMServer ("Excel.Application")
MsgBox "It might take a few seconds before something happens" & Chr(10) & _
"(Excel-Startups are not the fastest, especially on a 'cold FileCache')"
ShowCOMErrors = False 'we check the COMErr-Variable (and show an Error-MsgBox when needed) ourselves
Dim xlApp As vbVariant = CreateObject("Excel.Application")
If COMErr.Number Then MsgBox COMErr.Description: END ' early exit on Machines without an Excel-Install
ShowCOMErrors = True 'for the rest of the snippet, we switch the built-in COMErr-Messaging back On
xlApp.Get("WorkBooks").Call("Add")
xlApp.Put("Visible", "b", True)
Dim xlSheet As vbVariant = xlApp.Get("ActiveSheet"), CellValue As vbVariant
With xlSheet.Get("Range", "w", "A1") 'let's operate from the TopLeft-cell
Dim SArr(0 To 2) As String = {"Hello", "COM-calls", "from FB"}
For i As Integer = 0 To UBound(SArr)
.Get("Offset", "ll", 0, i).Put("Value", "s", SArr(i))
Next
CellValue = .Get("Offset", "ll", 0, 1).Get("Value") '<- should return "COM-calls"
End With '<- at this point, the above instantiated A1-Range-Object will be destroyed implicitly
xlSheet.Get("Parent").Put("Saved", "b", True) 'let's suppress the "Save Document" Dialogue ...
xlSheet.Clear '... and destroy the Sheet-Object with the appropriate vbVariant.Method ...
MsgBox CellValue '... now show the CellValue we have retrieved (should contain "COM-calls") ...
xlApp.Call("Quit") '... before we "call it quits" here
(I'm not fully up to speed with FB yet)...
Have fun!
Olaf