SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Windows specific questions.
Post Reply
OSchmidt
Posts: 49
Joined: Jan 01, 2016 12:27
Contact:

SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by OSchmidt »

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

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
As always, hints to formulate some code-parts more efficiently are welcome
(I'm not fully up to speed with FB yet)...

Have fun!

Olaf
OSchmidt
Posts: 49
Joined: Jan 01, 2016 12:27
Contact:

Re: SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by OSchmidt »

For those who want to see, what SimpleVariant.bi (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):

http://www.freebasic.net/forum/viewtopi ... =6&t=24795

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

Re: SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by stephanbrunker »

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.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by kcvinu »

Hi,
Luckily, i just found this thread. I was searching for how to get already opened excel file with COM like we can do in vb.net'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 ?
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by kcvinu »

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

Re: SimpleVariant.bi (OleVariant-Wrapper with Dispatch-Support)

Post by srvaldez »

you are missing the Disphelper library, you can find some libraries for 32-bit FB here https://sourceforge.net/projects/fbc/fi ... Libraries/
specifically the disphelper lib https://sourceforge.net/projects/fbc/fi ... p/download
Post Reply