通过c / c ++ dylib函数指针指向VBA在mac上 [英] passing c/c++ dylib function taking pointer to VBA on mac

查看:156
本文介绍了通过c / c ++ dylib函数指针指向VBA在mac上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上玩接口c / c ++和Excel的Excel的VBA的mac。我如何设计,在dylib,函数接受指针作为参数或引用?还是数组?或者甚至简单的结构?在窗口下, VARIANT 让我做一切,但我不能诉诸于OS X(甚至linux下)。

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) :

我有以下代码配置:在tmp3class .h:

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

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

in tmp3class.cpp:

in tmp3class.cpp :

#include "./tmp3class.h"

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

和tmp3.cpp中:

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 ;
      }
}


$ b

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

然后在vba我这样做:

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

和函数 THESUM_VBA

and the function THESUM_VBA works perfectly well.

推荐答案

这是一个答案(感谢这个)在数组的情况下:传递一个数组(双)从c ++到excel-2011的(mac)VBA ,c ++函数在其签名中应该具有表示指向数组的第一个系数的指针的 double * int (或 long 或其他)来表示数组的大小。例如,采用数组并将其所有系数乘以参数值的函数将被编码如下:c ++代码是:

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

现在VBA应该引用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)

multarray 表示数组的第一个系数,,必须通过引用传递。下面是一个例子:

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


b $ b

上述链接包含许多其他有趣的示例,字符串( BSTR '),字符串数组,简单结构等,可以很容易地适应 gcc mac OS(/ linux)上下文。我想设计一个简单的 VARIANT 类来处理所有这些。

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.

这篇关于通过c / c ++ dylib函数指针指向VBA在mac上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆