How to pass an range directly to a C++ DLL/dylib on Excel for Mac?

Paulo Bueno 96 Reputation points
2021-06-27T04:49:53.577+00:00

Hi,

I'm trying to extend Excel using C++ on a Mac. I basically want to pass a range directly to a function written in a C++ dylib. So far I have been able to pass arrays of basic types such as Doubles and Integers successfully. However, looping through a big range and converting each of its elements to, for example, a Double, takes up some time an essentially kills the purpose of using C++ (performance). Even geting the .Value or .Value2 property seems to take up some unnecessary time for really big ranges.

So far I have been able to pass a range to C++ as VARIANT , but I can't do much more than getting its dimensions/number of elements of the underlying SAFEARRAY. I haven't been able to access the underlying data and haven't found any documentation that would help me doing that on a Mac.

PS: For context, I'm using Olorin's VARIANT.h implementation.
Thank you very much.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,774 questions
{count} votes

Accepted answer
  1. Paulo Bueno 96 Reputation points
    2021-06-28T20:36:52.277+00:00

    @RLWA32 @Viorel I am finally able to access the data in C++. Many thanks for the help!

    First, I was successfully able to get the the pointer to Variant Array inside VBA using VarPtr(), then pass it from VBA to C++ and access the data from there. After achieving that, I noticed that each VARIANT was 24 bytes apart from the previous one. I found this weird, because sizeof VARIANT returned 32 bytes (under the implementation I was using) in C++.

    I then proceeded to port everything from Windows myself. I noticed that, while a ULONG would be 8-bytes long on Mac, it was only 4-bytes long on my PC. So I made use of int32_t/int64_t wherever it was fit.

    Now I can get the data (almost) directly from a range in Excel! Just need to extract the .Value from it first, but for my use case that's good enough in terms of performance. I think I'll try to port the IDispatch interface now. That should be fun.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.