Ask Your Question
3

How can a Variant be sent from a c dll to Excel VBA on macOS?

asked 2022-05-17 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-11-17 20:00:00 +0000

scrum gravatar image

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()
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-17 11:00:00 +0000

Seen: 8 times

Last updated: Nov 17 '21