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()
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
Asked: 2022-05-17 11:00:00 +0000
Seen: 8 times
Last updated: Nov 17 '21
How can an iOS notification be parsed?
What are the flags specific to compilers used by Bazel?
Can Swift be configured in Xcode build settings like -ObjC?
In Xcode, how can I format code similar to Eclipse or NetBeans?
Can promiscuous mode be enabled on a MAC programmatically?
How can Objective-C and Python Kivy be used together to create a webview for iOS?
What is the purpose of using NSObject?
What are the differences between calling a function with ctypes and pyObjc?