Excel XLL Add-ins with twinBASIC (backwards compatible with VBA language)

fafalone

New Member
Joined
Dec 6, 2024
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
So I was told that this community would be interested in a way to make XLL addins using your VBA language skills rather than need to learn C/C++ or other entirely different languages.

If you haven't heard of twinBASIC before, its a backwards compatible successor to VB6, with VBA7 syntax for 64bit support, currently under development in late beta. FAQ

XLL addins are just renamed standard dlls, and tB supports creating these natively (Note: it can also make standard activex/com addins for Office apps, and ocx controls). So I went ahead and ported the Excel SDK definitions from xlcall.h to tB, then ported a simple Hello World addin as a proof of concept it's possible to make these without too much difficulty:

VBA Code:
    [DllExport]
    Public Function xlAutoOpen() As Integer
 
        Dim text As String = StrConv("Hello world from a twinBASIC XLL Addin!", vbFromUnicode)
        Dim text_len As Long = Len("Hello world from a twinBASIC XLL Addin!")
        Dim message As XLOPER
        message.xltype = xltypeStr
       
        Dim pStr As LongPtr = GlobalAlloc(GPTR, text_len + 2) 'Excel frees it, that's why this trouble
        CopyMemory ByVal VarPtr(message), pStr, LenB(pStr)
        CopyMemory ByVal pStr, CByte(text_len), 1
        CopyMemory ByVal pStr + 1, ByVal StrPtr(text), text_len + 1
 
        Dim dialog_type As XLOPER
        dialog_type.xltype = xltypeInt
        Dim n As Integer = 2
        CopyMemory ByVal VarPtr(dialog_type), n, 2
 
        Excel4(xlcAlert, vbNullPtr, 2, ByVal VarPtr(message), ByVal VarPtr(dialog_type))
        Return 1
    End Function

Pretty much all the difficulty is dealing with that nightmarish XLOPER type. It's full of unions and internal structs neither VBx nor tB (yet) supports. So I substituted LongLong members to get the right size and alignment, then fortunately the main union is the first member so all data is copied to VarPtr(XLOPER). Assigning it without CopyMemory would be at the wrong spot in memory most of the time because of how unions are laid out internally.

So a little complicated, and I did use some of tB's new syntax/features, but still way more accessible than C/C++ imo!

For complete details on how and full source code, check out the project repository:

 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
TwinBasic is already opening up new possibilities for both Excel and VBA users. I had no idea that this was also a possibility. Great job.
 
Upvote 0
I've just seen this. Thanks fafalone ... This will be very useful.
 
Upvote 0
So my initial proof of concept wasn't very useful, just showing a messagebox on load. I've now made a followup which shows how to create User-Defined Functions in XLLs. Additionally, I've added helper functions to the SDK to wrap many of the gory details of handling XLOPER12 types, especially for Strings. XLL UDFs directly execute native compiled code, making them substantially faster than the P-Code interpreter that runs regular Office VBA. Once twinBASIC supports LLVM optimization in the near future, it will go from 'substantially faster' to 'completely blows it out of the water'.


There's a much more detailed writeup in the GitHub repo.


GitHub - fafalone/TBXLLUDF: twinBASIC XLL UDF Addin Example
 
Upvote 0

Forum statistics

Threads
1,225,364
Messages
6,184,534
Members
453,239
Latest member
dbenthu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top