Re: Excel
- Posted by Mike777b Oct 11, 2010
- 1522 views
One issue with this is that he wants to pass a double. Euphoria callbacks can't handle doubles being natively passed. They can only take 31-bit integers (or Euphoria Atoms or Sequences).
The desired signature is:
Public Declare Function MyFunction Lib "c:\MyDLLs\MyDLL.dll" _ (ByVal a As Double, ByVal b As Double) As Double
It was just an example. I can live with 31 bit values if I have to (I can get a date or a value up to 21,474,836.48, right?).
If you translate (I did this on Linux, but should be similar), you should get some thing like :
$ euc -makefile-full -dll vb.e // inside vb.c: int _1MyFunction(int _a_144, int _b_145) { int _c_146 = 0; int _0, _1, _2; /** atom c = a * b*/ if (IS_ATOM_INT(_a_144) && IS_ATOM_INT(_b_145)) { if (_a_144 == (short)_a_144 && _b_145 <= INT15 && _b_145 >= -INT15) _c_146 = _a_144 * _b_145; else _c_146 = NewDouble(_a_144 * (double)_b_145); } else { if (IS_ATOM_INT(_a_144)) { _c_146 = NewDouble((double)_a_144 * DBL_PTR(_b_145)->dbl); } else { if (IS_ATOM_INT(_b_145)) { _c_146 = NewDouble(DBL_PTR(_a_144)->dbl * (double)_b_145); } else _c_146 = NewDouble(DBL_PTR(_a_144)->dbl * DBL_PTR(_b_145)->dbl); } } /** return c*/ DeRef(_a_144); DeRef(_b_145); return _c_146; ; }
The easiest thing to do here is probably to write a wrapper function right below the above code:
When you say the "above code", what do you mean? In what language is the wrapper function (I only do EU, so if it is C, I'll be stuck).
double MyFunctionDbl( double ad, double bd ){ object a, b, c; double cd; a = NewDouble( ad ); b = NewDouble( bd ); c = _1MyFunction( a, b ); cd = DBL_PTR( c )->dbl; DeRefDS( a ); DeRefDS( b ); DeRefDS( c ); printf("MyFunctionDbl ad %f bd %f cd %f\n", ad, bd, cd ); return cd; }
And then call that. I wrote this test program to use it:
include std/dll.e constant VB = open_dll( "/home/matt/eu/test/vb.so" ), MYFUNC = define_c_func( VB, "MyFunctionDbl", {C_DOUBLE, C_DOUBLE}, C_DOUBLE ) ? c_func( MYFUNC, { 0.5, 0.25 }) ? c_func( MYFUNC, {1, 1 })
This is testing whether the DLL works from EU, right? Does that necessarily translate into success if the DLL is being called from VBA within Excel?
Thanks
mike