(OleVariant-Wrapper with Dispatch-Support)

Windows specific questions.
Posts: 47
Joined: Jan 01, 2016 12:27
Contact: (OleVariant-Wrapper with Dispatch-Support)

Postby OSchmidt » Jun 15, 2016 1:46

Ok, my second attempt at writing a small, but powerful LateBound-Helper for COM-Objects... ;-)

Working on more complex examples for
it became apparent that without a Variant-Wrapper-Type (inlcuding proper Assignment- and Cast-Methods), "there's just no fun"...

So, here is, 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 ... includes all the functionality from (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:
It includes (along with 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 ""

'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.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))
        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

As always, hints to formulate some code-parts more efficiently are welcome
(I'm not fully up to speed with FB yet)...

Have fun!

Posts: 47
Joined: Jan 01, 2016 12:27

Re: (OleVariant-Wrapper with Dispatch-Support)

Postby OSchmidt » Jun 22, 2016 2:57

For those who want to see, what (as a small, generic Dispatch-implementation-module)
is already able to carry - I've just posted an example, where it is used as the level-0 layer for
(latebound) FB-Wrapper-objects (in a concrete binding to a mid-sized COM-Framework):


Posts: 62
Joined: Nov 02, 2013 14:57

Re: (OleVariant-Wrapper with Dispatch-Support)

Postby stephanbrunker » Oct 22, 2017 17:15

First, thank you for your work!

I use SimpleVariant for a COM connection to hMailServer, but there I ran into an issue. For example, the Authenticate() Function returns an Object if successful, and also an Object containing 0 on failure. In other languages, you can simply query that with

Code: Select all

If returnVal == NULL Then ...

With SimpleVariant, you get an COM Cast Error, because it tries to Cast the Object first to Integer to check that condition. After I looked into the details, it is simple: The tagVariant Datatype consists of four Short values, the first is the datatype, the next three are empty. Next is an union, containing the data. The biggest datatype and thus the size of the allocation is Longint, 64bit. So, for an vbVariant.IsNull() Function, you have only to check if this value is 0. I did this with an IsNull() Function, PHP style:

Code: Select all

Function vbVariant.IsNull() As Boolean
    Dim pT as Short Ptr = CPtr(Short Ptr, @V)
    Dim pUl as Longint Ptr = CPtr(Longint Ptr, @pt[4])
    Return *pUl = 0
End Function

I hope this helps if anybody has an similar problem.
Posts: 173
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: (OleVariant-Wrapper with Dispatch-Support)

Postby kcvinu » Sep 04, 2018 13:53

Luckily, i just found this thread. I was searching for how to get already opened excel file with COM like we can do in's "GetActiveObject()" function. But i am having hard time, so that i cant find the CoGetObjet in your *.bi file. Could youlease point me to the right direction ?
Posts: 173
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: (OleVariant-Wrapper with Dispatch-Support)

Postby kcvinu » Sep 06, 2018 18:07

When using disphelper, compiler says that it cannot find IDisphelper.
Posts: 1835
Joined: Sep 25, 2005 21:54

Re: (OleVariant-Wrapper with Dispatch-Support)

Postby srvaldez » Sep 06, 2018 18:36

you are missing the Disphelper library, you can find some libraries for 32-bit FB here ... Libraries/
specifically the disphelper lib ... p/download

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 1 guest