I have a bunch of D functions I would like to make available to Excel (and possibly Julia) without having to write wrappers for each function individually.

For Excel, I think one needs two levels of wrapper - one is to create a C style interface [using extern(Windows) calling convention, and pointers to doubles or structs rather than dynamic arrays], and the second is to write the VBA wrapper that calls the C interface. (There may be more efficient purer ways of doing this, but I don't wish to spend time learning Excel internals/object models, and I know my route will work reasonably well).

So a very simple D function:
double test(double[] inp,  ref double[] oup)
{
        double sum=0.0;
        oup.length=inp.length;
        foreach(i;0..inp.length)
        {
                oup[i]=inp[i]*inp[i];
                sum+=oup[i];
        }
        return sum;
}

and my first attempt at a wrapper:

extern(Windows) double vbwrap_test(double* inp,size_t num_inp,double* oup,size_t num_oup)
{
        double[] arg_inp;
        arg_inp.length=num_inp;
        double[] arg_oup;
        arg_oup.length=num_oup;
        foreach(arg;0..num_inp)
        {
                arg_inp[arg]=inp[arg];
        }

        foreach(arg;0..num_oup)
        {
                arg_oup[arg]=oup[arg];
        }

        return test(arg_inp,arg_oup);
}

I didn't yet write the bit that copies the result from test back to the calling double*.

Slowly learning metaprogramming/CTFE in D, and the code above was generated from the function definition by some horrible looking D code, ready to place into a string mixin. I need to make it more general (to accept structs etc), and write the VBA wrapper generation too.

But if anyone has any useful pointers or suggestions or would like to help, do let me know. I guess this project could be of broader application since in the financial and other sectors people still are stuck with Excel as a front end in many cases, for better or for worse.

I will look at LuaD and PyD and Adam's web.d for inspiration..

Julia was just something to think about further down the line. I haven't used it much yet.


Thanks.


Laeeth.

Reply via email to