Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Unfortunately, there isn't a straightforward way to send a Variant from a C DLL to Excel VBA on macOS. This is because macOS uses a different architecture than Windows, and there are differences in the way that DLLs are handled between the two platforms.

However, you may be able to achieve the same result by creating a wrapper function in Objective-C or Swift that calls the C DLL function and returns the data in a format that Excel VBA can understand. Here's an example implementation in Objective-C:

#include <stdio.h>
#include <stdlib.h>

// Declare the C DLL function that returns a Variant
extern void __declspec(dllimport) MyDLLFunction(VARIANT* variant);

// Create a wrapper function that calls the C DLL function and returns the data as an NSString
NSString* MyWrapperFunction() {
    VARIANT variant;
    VariantInit(&variant);
    MyDLLFunction(&variant);

    // Convert the VARIANT data to an NSString
    BSTR bstr;
    HRESULT hr = VariantChangeType(&variant, &variant, 0, VT_BSTR);
    if (SUCCEEDED(hr)) {
        bstr = variant.bstrVal;
    }
    NSString* str = [NSString stringWithUTF8String:bstr];

    // Clean up the VARIANT data
    VariantClear(&variant);

    return str;
}

// Export the wrapper function so that Excel VBA can call it
__attribute__((visibility("default")))
NSString* MyExportedFunction() {
    return MyWrapperFunction();
}

You would need to compile this code into a dynamic library (e.g. MyLib.dylib) and then import it into Excel VBA using the Declare statement:

Declare Function MyExportedFunction Lib "/path/to/MyLib.dylib" () As String

Once imported, you can call the function from your VBA code to retrieve the data:

Dim myData As Variant
myData = MyExportedFunction()