passing c/c++ dylib function taking pointer to VBA on mac

六月ゝ 毕业季﹏ 提交于 2019-12-25 11:57:08

问题


I am actually playing with interfacing c/c++ and the VBA of Excel-2011 for mac. How could I design, in the dylib, functions taking pointers as parameters, or references ? Or arrays ? Or even simple structs ? Under windows, VARIANT let's me do everything, but I cannot resort to it under OS X (or even under linux).

Just as comment, up to now, I can do things like these for (involving "simple" types) :

I have the following code configuration : in tmp3class.h :

class TheClass
{
      public:
            double mysum(double x ,double y);
};

in tmp3class.cpp :

#include "./tmp3class.h"

double TheClass::mysum(double x ,double y)
{
      return x+y ;
}

and in tmp3.cpp :

#include "./tmp3class.h"

extern "C"
{
      double THESUM(double x, double y)
      {
            TheClass TheObj ;
            double res = TheObj.mysum(x,y);
            return res ;
      }
}

I compile this with :

g++-5.2.0 -m32 -Wall -g -c ./tmp3class.cpp -o ./tmp3obj.o
g++-5.2.0 -m32 -dynamiclib .tmp3.cpp ./tmp3obj.o -o ./tmp3.dylib

and then in the vba I do this :

Declare Function THESUM Lib "/Users/XXXXXX/Documents/GITHUBRepos/DYLIBS/MyFirstDylib/tmp3.dylib" (ByVal x As Double, ByVal y As Double) As Double

Function THESUM_VBA(x As Double, y As Double) As Double
    THESUM_VBA = THESUM(x, y)
End Function

and the function THESUM_VBA works perfectly well.


回答1:


Here is an answer (designed thank to this) in the case of arrays : to pass an array (of double) from c++ to excel-2011's (mac) VBA, the c++ function should have in its signature a double * representing the pointer to the first coefficient of the array, an int (or a long or etc) reprensenting the size of the array. For instance, a function taking an array and multiplying all its coefficients by a parameter value would be coded like this : the c++ code is :

extern "C"
{
      void multarray(double * array, int size, double coeff)
      {
            for (int i = 0 ; i < size ; ++i)
            {
                  array[i]*=coeff;
            }
      }
}

compiled with :

g++ -m32 -Wall -g -c ./tmp4.cpp
g++ -m32 -dynamiclib ./tmp4.o -o ./tmp4.dylib

Now the VBA should reference the dylib as follows :

Declare Sub multarray Lib "/Users/XXXXXX/Documents/GITHUBRepos/DYLIBS/MyFirstDylib/tmp4.dylib" (ByRef firstcoeff As Double, ByVal size As Long, ByVal coeff As Double)

The first parameter of multarray represent the first coefficient of the array, and must be passed by reference. Here is an exemple of utilisation :

Public Sub DoIt()
    Dim multarraofdoubles(3) As Double
    multarraofdoubles(0) = -1.3
    multarraofdoubles(1) = 4.6
    multarraofdoubles(2) = -0.67
    multarraofdoubles(3) = 3.13
    Dim coeff As Double
    coeff = 2#
    Call multarray(multarraofdoubles(0), 4, coeff)
End Sub

The aforementioned link contains many other interesting examples, for strings (with BSTR's), array of strings, simple structs etc, that could be easily adapted to gcc and this mac OS (/linux) context. I am thinking of designing a simple VARIANTclass handling all of this.



来源:https://stackoverflow.com/questions/32237575/passing-c-c-dylib-function-taking-pointer-to-vba-on-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!